Edit a ShapeFile with ArcObjects Programatically

3573
1
03-08-2011 11:34 AM
BryanKardisco
New Contributor
I currently have a basic shapefile that I want to edit in ArcObjects in C#.

I've created a shape file by creating a FeatureClass , Workspace and then obviously passing in my shape.  In this case it's just a simple Polyline with a few points on it.  What I'd like to do is have the ability to update this shapefile.

I'm assuming I need to re-use that FeatureClass, Workspace and pass in a new shape.  However, what I'm doing doesn't seem to work.  This is where I'm at on the edit - any advice would be great.

IWorkspaceEdit workspaceEdit = (IWorkspaceEdit)inWorkspace;
workspaceEdit.StartEditing(true);
workspaceEdit.StartEditOperation();

ComReleaser comReleaser = new ComReleaser();

IFeatureCursor featureCursor = inFeatureClass.Update(null , true);

IFeature feature = null;
while((feature = featureCursor.NextFeature()) != null)
{
   int id = feature.Fields.FindField("shape");
   IFeatureBuffer featureBuffer = inFeatureClass.CreateFeatureBuffer();
   featureCursor.DeleteFeature(); //Delete the old
   featureBuffer.Shape = (IGeometry)inShape;
   featureCursor.InsertFeature(featureBuffer);
}

workspaceEdit.StopEdidtOperation();
workspaceEdit.StopEditing(true);
0 Kudos
1 Reply
AlexanderGray
Occasional Contributor III
Hi
A few things I noticed.  You probably should use a non-recycling cursor for updating.  I am not sure why you are trying to delete a feature and create a new one with the exact same shape.  You can update the features geometry directly with an update cursor through the shape property.  If the code you provided worked, the only thing I could see it doing is changing all the objectids and setting any attributes to default, the shapes will be the same.  If that is what you want to do, I would advise to let ArcObjects handle the ObjectId, and you can change the attributes of the feature with an update cursor without deleting and creating the features. If you truly want to set a feature's shape to another feature, I would suggest using shapecopy.  I would also recommend you add the cursor to the comreleaser (managelifetime)
0 Kudos