Select to view content in your preferred language

Delete a shapefile

4540
6
01-28-2011 06:45 AM
MarkParr
Emerging Contributor
Using ArcEngine, is there a call that I can pass the name of SHAPEFILE.SHP and have all the files associated w/ a shapefile deleted from disk?  If so, anyone have an example of the process.

I found the Delete class under ESRI.ArcGIS.DataManagementTools namespace that sounds like what I was looking for but no good examples of how to implement it.

Thanks
0 Kudos
6 Replies
RuchiraWelikala
Regular Contributor
This is the old VBA code to delete the shapefile.  Add the appropriate references and modify the code to suit your needs in .NET.

Private Sub DeleteDataset()
Dim pMxDocument As IMxDocument
Dim pMap As IMap
Dim pFeatureLayer As IFeatureLayer
Dim pFeatureClass As IFeatureClass
Dim pDataset As IDataset
Dim pActiveView As IActiveView
Set pMxDocument = ThisDocument
Set pMap = pMxDocument.FocusMap
' Define the dataset to be deleted.
Set pFeatureLayer = pMap.Layer(0)
Set pFeatureClass = pFeatureLayer.FeatureClass
' Remove the layer from the active map.
pMap.DeleteLayer pFeatureLayer
' Delete the dataset.
Set pDataset = pFeatureClass
pDataset.Delete
' Refresh the map.
Set pActiveView = pMap
pActiveView.Refresh
End Sub


Cheers,
0 Kudos
MarkParr
Emerging Contributor
After posting I found several references to deleting shapefiles on the older, retired forum which I think the VBA code you show was referenced.  I also found a reference to some code where the various file extensions of the files associated to a shapefile were stepped thru and deleted. 

The shapefile that I want to delete from disk will not be opened at the time.  Looking at this VBA code it appears to be looking for an active layer to remove it from the map
0 Kudos
RuchiraWelikala
Regular Contributor
That's correct.  The code above needs a layer assigned..but since you don't need that, try this code.  It's actually a snippet in included with the ESRI .NET SDK so it should be in your VS IDE.

        ' This snippet is intended to be inserted at the base level of a Class.
    ' It is not intended to be nested within an existing Function or Sub.
    ' 

    '''<summary>Deletes from a specified directory that match a certain pattern.</summary>
    ''' 
    '''<param name="filePath">A System.String that is the file and path from where to delete files. Example: "C:\temp"</param>
    '''<param name="pattern"> A System.String that is the pattern for files to delete. Example: "*.txt" or "myfile.*" or "*.*"</param>
    '''  
    '''<remarks>All files meeting the pattern will be deleted from the specified directory.</remarks>
    Public Sub DeleteFilesFromDir(ByVal filePath As System.String, ByVal pattern As System.String)

        Dim directoryInfo As System.IO.DirectoryInfo = New System.IO.DirectoryInfo(filePath)
        Dim files As System.IO.FileInfo() = directoryInfo.GetFiles(pattern)
        For Each file As System.IO.FileInfo In files
            file.Delete()
        Next

    End Sub
0 Kudos
MarkParr
Emerging Contributor
Thanks for the suggestion.  That basic code appears to get everthing so it will need to be tweaked for just the files that make up the shapefile.  If I call DeleteShapefile(SHAPEFILE.SHP), I want to get that file and the .DBF, PRJ, etc but I don't want to delete SHAPEFILE.ZIP that might be out there.

I was hoping for a built-in ArcEngine call that would have handled that for me but I can work around that.

Thanks
0 Kudos
NeilClemmons
Honored Contributor
Use IDataset.Delete as shown in the original reply.  Instead of getting a layer from the map just open the shapefile yourself using IFeatureWorkspace.OpenFeatureClass.
0 Kudos
MarkParr
Emerging Contributor
Neil:

Thanks for the direction -- that's what I was looking for.
0 Kudos