This document is archived and information here might be outdated.  Recommended version.


Delete Files from Directory Snippet (ArcObjects .NET 10.4 SDK)
ArcObjects Library Reference

Delete Files from Directory Snippet

Deletes from a specified directory that match a certain pattern.

[C#]
///<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 void DeleteFilesFromDir(System.String filePath, System.String pattern)
{
  System.IO.DirectoryInfo directoryInfo=new System.IO.DirectoryInfo(filePath);
  System.IO.FileInfo[] files=directoryInfo.GetFiles(pattern);
  foreach (System.IO.FileInfo file in files)
  {
    file.Delete();
  }
}
[Visual Basic .NET]
'''<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

Additional Requirements
  • The code in this document requires the following References added to the Visual Studio project:
  • System