How to create a new feature class using ArcObjects
IGxDialog gxDialog;
IWorkspace ws;
IFields fields;
IFieldsEdit fieldsEdit;
IField field;
IFieldEdit fieldEdit;
IGeometryDefEdit geomDefEdit;
IFeatureClass fc;
// create an ArcMap dialog for selecting a new location
gxDialog = new GxDialogClass();
gxDialog.ObjectFilter = new GxFilterPolygonFeatureClassesClass();
// return if cancelled
if (!gxDialog.DoModalSave(0)) return;
if (gxDialog.FinalLocation is IGxDatabase)
{
// if the user selected a workspace
ws = ((IGxDatabase)gxDialog.FinalLocation).Workspace;
}
else if (gxDialog.FinalLocation is IGxDataset)
{
// if the user selected a dataset
ws = ((IGxDataset)gxDialog.FinalLocation).Dataset.Workspace;
}
else if (gxDialog.FinalLocation is IGxFolder)
{
// ((IGxFolder)gxDialog.FinalLocation).FileSystemWorkspaceNames returns no names; not sure why
// create a shapefile workspace factory
IWorkspaceFactory wsFact;
IName wsName;
wsFact = new ShapefileWorkspaceFactoryClass();
wsName = (IName)wsFact.Create(System.IO.Path.GetDirectoryName(gxDialog.FinalLocation.FullName),
System.IO.Path.GetFileName(gxDialog.FinalLocation.FullName), null, 0);
ws = (IWorkspace)wsName.Open();
}
else
{
// unsupported final location
throw new ApplicationException(string.Format("Unsupported final location category: {0}",
gxDialog.FinalLocation.Category));
}
// check if the user-specified name already exists
if (((IWorkspace2)ws).NameExists[esriDatasetType.esriDTFeatureClass, gxDialog.Name])
{
IEnumDataset enumDS;
IDataset ds;
if (MessageBox.Show(string.Format("{0}: Already exists. Do you want to overwrite it?", gxDialog.Name),
"Warning", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.No) return;
enumDS = ws.Datasets[esriDatasetType.esriDTFeatureClass];
while ((ds = enumDS.Next()) != null)
{
if ((ds.Name + (gxDialog.FinalLocation is IGxFolder ? ".shp" : "")).ToUpper() == gxDialog.Name.ToUpper())
{
if (ds.CanDelete())
{
ds.Delete();
}
else
{
throw new ApplicationException(string.Format("{0}: Cannot delete", gxDialog.Name));
}
}
}
}
// create required fields
fields = new FieldsClass();
fieldsEdit = (IFieldsEdit)fields;
// create an OID field
field = new FieldClass();
fieldEdit = (IFieldEdit)field;
fieldEdit.Name_2 = "ObjectID";
fieldEdit.Type_2 = esriFieldType.esriFieldTypeOID;
fieldEdit.Required_2 = true;
fieldEdit.IsNullable_2 = false;
// add the OID field
fieldsEdit.AddField(field);
// define geometry
geomDefEdit = (IGeometryDefEdit)new GeometryDefClass();
geomDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPolygon;
geomDefEdit.HasM_2 = false;
geomDefEdit.HasZ_2 = false;
geomDefEdit.SpatialReference_2 = yourSpatialReference;
// create a geometry field
field = new FieldClass();
fieldEdit = (IFieldEdit)field;
fieldEdit.Name_2 = "Shape";
fieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry;
fieldEdit.GeometryDef_2 = geomDef;
fieldEdit.Required_2 = true;
fieldEdit.IsNullable_2 = false;
// add the geometry field
fieldsEdit.AddField(field);
// create a new feature class
fc = ((IFeatureWorkspace)ws).CreateFeatureClass(gxDialog.Name, fields, null, null,
esriFeatureType.esriFTSimple, "Shape", "");