ArcObjects – How to Overwrite Shapefile or Feature Class

arcgis-10.0arcobjects

I can copy a shapefile but I want to be able to overwrite the copied shapefile to prevent multiple copies being created each time I run the program. I would also like for the saving and renaming of the copied shapefile to happen behind the scenes i.e. the user doesnt see a gxDialog box.

How do I overwrite the copied file, either as a shapefile or feature class?

Best Answer

Before making the copy of the feature class, you can open the containing workspace and check the existance of the feature class (IWorkspace2.NameExists), and if exists then delete it (IDataset.Delete).

Example:

Dim pWorkspace2 as IWorkspace2
set pWorkspace2 = yourWorkspace

' Checks the existance of the feature class
If pWorkspace2.NameExists(yourDestFeatureClass) Then

   Dim pFeatureWorkspace as IFeatureWorkspace
   set pFeatureWorkspace = yourWorkspace

   ' Open the feature class and cast it to the IDataset interface
   Dim pDataset as IDataset
   set pDataset = pFeatureWorkspace.OpenFeatureClass(yourDestFeatureClass)

   ' Removes the feature class
   pDataset.Delete

End if

' Your code to make the copy...
Related Question