C# delete file if exists and delete all files in folder, with the specified files and folders

Lionsure 2020-08-28 Original by the website

There are different deletion methods according to different deletion requirements. If you want to delete all subfolders and files in a folder, you can delete this folder; if you want to delete a specified file or folder in a folder, you have to iterate and delete it.

For different needs, different code examples are given below to meet the various needs of different users, starting with the simplest.

 

I. C# delete file if exists

1. Physical path

public void DeleteFileIfExists(string path)
       {
              if (File.Exists(path))
                     File.Delete(path);
       }

Call: DeleteFileIfExists(@"F:\temp\test.txt");

 

2. Virtual path

public void DeleteFileIfExistsVirtualPath(string path)
       {
              path = Server.MapPath(path);//Convert virtual path to physical path
              if (File.Exists(path))
                     File.Delete(path);
       }

Call: DeleteFileIfExistsVirtualPath("/htm/test.html");

 

 

II. C# delete all files in folder

/// <summary>
       /// C# delete all files and subfolders in a folder
       /// </summary>
       /// <param name="dirPath">Path of the folder to be deleted</param>
       /// <returns>Delete return true, otherwise return false</returns>

       private bool DelForlder(string dirPath)
       {
              if (Directory.Exists(dirPath))
              {
                     DirectoryInfo di = new DirectoryInfo(dirPath);
                     di.Delete(true);//true means delete subdirectories and files
                     return true;
              }
              return false;
       }

Call: DelForlder(@"E:\files\test");

 

III. C# delete all files in folder

/// <summary>
       /// C# delete all files in directory
       /// </summary>
       /// <param name="dirPath">Path of the folder to be deleted</param>
       /// <returns>Delete all files return true, otherwise return false</returns>

       private bool DelFiles(string dirPath)
       {
              DirectoryInfo di = new DirectoryInfo(dirPath);
              FileInfo[] arrFi = di.GetFiles("*.*");
              try
              {
                     foreach (FileInfo fi in arrFi)
                     {
                            File.Delete(di + "\\" + fi.Name);
                     }
                     returntrue;
              }
              catch
              {
                     return false;
              }
       }

Call: DelFiles(@"E:\files\test");

 

IV. C# delete all subfolders in a folder in batches

/// <summary>
       /// C# delete all subfolders in a folder
       /// </summary>
       /// <param name="dirPath">Path of the folder to be deleted</param>
       /// <returns>Delete all folders return true, otherwise return false</returns>

       private bool DelFolders(string dirPath)
       {
              DirectoryInfo di = new DirectoryInfo(dirPath);
              DirectoryInfo[] arrDi = di.GetDirectories();
              try
              {
                     foreach (DirectoryInfo dirInfo in arrDi)
                     {
                            Directory.Delete(di + "\\" + dirInfo.ToString(), true);
                     }
                     return true;
              }
              catch
              {
                     return false;
              }
       }

Call: DelFolders(@"G:\files\test");

 

V. C# delete filess to be pecified in the folder in batches

/// <summary>
       /// C# delete filess to be pecified in the folder
       /// </summary>
       /// <param name="dirPath">Path of the folder to be deleted</param>
       /// <param name="files">File array to be deleted</param>
       /// <returns>Delete all specified files return true, otherwise return false</returns>

       private bool DelAppointFiles(string dirPath, string[] files)
       {
              DirectoryInfo di = new DirectoryInfo(dirPath);
              FileInfo[] arrFi = di.GetFiles("*.*");
              try
              {
                     foreach (FileInfo fi in arrFi)
                     {
                            foreach (string s in files)
                            {
                                   if(fi.Name.Equals(s))
                                   File.Delete(di + "\\" + fi.Name);
                            }
                     }
                     return true;
              }
              catch
              {
                      return false;
              }
       }

Call:

string[] arr = { "test.txt1", "test.txt2" };

DelAppointFiles(@"E:\files\test", arr);