// Generate a new shapefile
IFields pFields = new FieldsClass ();
IFieldsEdit pFieldsEdit = pFields as IFieldsEdit;
IField pField = new FieldClass ();
IFieldEdit pFieldEdit = pField as IFieldEdit;
PFieldEdit. Name_2 = "shape ";
PFieldEdit. Type_2 = esriFieldType. esriFieldTypeGeometry;
IGeometryDef pGeometryDef = new GeometryDefClass ();
IGeometryDefEdit pGeoDefEdit = pGeometryDef as IGeometryDefEdit;
PGeoDefEdit. GeometryType_2 = esriGeometryType. esriGeometryPolygon;
PGeoDefEdit. SpatialReference_2 = axMapControl1.SpatialReference;
PFieldEdit. GeometryDef_2 = pGeometryDef;
PFieldsEdit. AddField (pField );
PField = new FieldClass ();
PFieldEdit = pField as IFieldEdit;
PFieldEdit. Length_2 = 10;
PFieldEdit. Name_2 = "CODE ";
PFieldEdit. AliasName_2 = "CODE ";
PFieldEdit. Type_2 = esriFieldType. esriFieldTypeString;
PFieldsEdit. AddField (pField );
String MapPath = @ "E: \ data \ result ";
String LayerName = "AddFeature ";
IWorkspaceFactory pWsf = new ShapefileWorkspaceFactory ();
IFeatureWorkspace pFws = pWsf. OpenFromFile (MapPath, 0) as IFeatureWorkspace;
IFeatureClass pFeatureClass = pFws. CreateFeatureClass (LayerName + ". shp", pFields, null, null, esriFeatureType. esriFTSimple, "shape ","");
Create shapefile, Custom field, and path customization.
Add POLYGON to the file to customize the field value
IDataset pDataset = (IDataset) pFeatureClass;
PFws = (IFeatureWorkspace) pDataset. Workspace;
IWorkspaceEdit pWorkspaceEdit = (IWorkspaceEdit) pFws;
PWorkspaceEdit. StartEditing (true );
PWorkspaceEdit. StartEditOperation ();
IFeatureBuffer pFeatureBuffer = pFeatureClass. CreateFeatureBuffer ();
// Create insert Feature Cursor using buffering = true.
IFeatureCursor pFeatCursor = pFeatureClass. Insert (true );
Object featureOID;
// Define the value of the newly added element
PFeatureBuffer. set_Value (pFeatureBuffer. Fields. FindField ("CODE"), "CODE ");
// Generate a Polygon ************************************ **********************
Ipolympus Gon poly = new PolygonClass ();
IPoint pPoint = new PointClass ();
Object o = Type. Missing;
IPointCollection pPc = new MultipointClass ();
PPc = (IPointCollection) poly;
For (int I = 0; I <= 0; I ++)
{
PPoint. PutCoords (200,400 );
PPc. AddPoint (pPoint, ref o, ref o );
PPoint. PutCoords (400,400 );
PPc. AddPoint (pPoint, ref o, ref o );
PPoint. PutCoords (400,200 );
PPc. AddPoint (pPoint, ref o, ref o );
PPoint. PutCoords (200,200 );
PPc. AddPoint (pPoint, ref o, ref o );
}
// After the generation is completed ************************************ ************************
PFeatureBuffer. Shape = poly;
FeatureOID = pFeatCursor. InsertFeature (pFeatureBuffer );
// Flush the feature cursor to the database
// Calling flush allows you to handle any errors at a known time rather then on the cursor destruction.
PFeatCursor. Flush ();
PWorkspaceEdit. StopEditOperation ();
PWorkspaceEdit. StopEditing (true );
// Release Cursor
System. Runtime. InteropServices. Marshal. ReleaseComObject (pFeatCursor );
AxMapControl1.AddShapeFile (MapPath, LayerName + ". shp ");