C# Delete a many files and sub folders with multiple threads

T.Zacks 3,926 Reputation points
2022-06-02T17:59:48.923+00:00

suppose i have many files in multiple folder and i have list of folders from where files will be deleted by multiple thread. i got a interesting code but not sure does it work perfectly if i use it. see the code

List<Task> tasks =
(from c in yourDirList select new Action(() =>
 {
  try
  {
   // do it
  }
  catch (Exception e)
  {
   // log it
  }
 })
 into action
 select Task.Factory.StartNew(action)).ToList();

tasks.ForEach(c => c.Wait());

1) how to get file path with in try block to delete?
2) below fragment of code is not clear....please help me to understand.

     into action
     select Task.Factory.StartNew(action)).ToList();

above code i got from here https://stackoverflow.com/a/5626373/18531882

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
6,952 questions
{count} votes

Accepted answer
  1. Jack J Jun 19,061 Reputation points Microsoft Employee
    2022-06-03T02:26:31.07+00:00

    @T.Zacks , Welcome to Microsoft Q&A,

    1) how to get file path with in try block to delete?

    You could try the following code to get the file path and directory path by using DirectoryInfo.GetFiles and DirectoryInfo.GetDirectories method.
    Also, We could use Directory.Delete method to delete the directory in the list.

    List<Task> tasks =  
     (from c in yourDirList  
      select new Action(() =>  
      {  
          try  
          {  
              DirectoryInfo di = new DirectoryInfo(c);  
              foreach (FileInfo file in di.GetFiles())  
              {  
                  file.Delete();  
              }  
              foreach (DirectoryInfo dir in di.GetDirectories())  
              {  
                  dir.Delete(true);  
              }  
              // do it  
              Directory.Delete(c);  
          }  
          catch (Exception e)  
          {  
              // log it  
          }  
      })  
      into action  
      select Task.Factory.StartNew(action)).ToList();  
    

    2) below fragment of code is not clear....please help me to understand.

    First, the code will select new Action create an Action delegate in every loop, we could use into action store every Action delegate into action.

    Second, the code Task.Factory.StartNew(action)) will create a Task based on the action.

    Finally, select keyword and Tolist() method will return a Task list.

    The next is my completed code, which could delete all the files and directories in the yourDirList.

     static void Main(string[] args)  
            {  
    
                var yourDirList= new List<string>();  
                yourDirList.Add(@"C:\Users\username\Desktop\Test1");  
                yourDirList.Add(@"C:\Users\username\Desktop\Test2");  
                yourDirList.Add(@"C:\Users\username\Desktop\Test3");  
                List<Task> tasks =  
     (from c in yourDirList  
      select new Action(() =>  
      {  
          try  
          {  
              DirectoryInfo di = new DirectoryInfo(c);  
              foreach (FileInfo file in di.GetFiles())  
              {  
                  file.Delete();  
              }  
              foreach (DirectoryInfo dir in di.GetDirectories())  
              {  
                  dir.Delete(true);  
              }  
              // do it  
              Directory.Delete(c);  
          }  
          catch (Exception e)  
          {  
              // log it  
          }  
      })  
      into action  
      select Task.Factory.StartNew(action)).ToList();  
    
                tasks.ForEach(c => c.Wait());  
            }  
    

    Hope my explanation could be helpful.

    Best Regards,
    Jack


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


3 additional answers

Sort by: Most helpful
  1. 2022-06-03T13:50:30.353+00:00

    1) how to get file path with in try block to delete?

    You could try the following code to get the file path and directory path by using DirectoryInfo.GetFiles and DirectoryInfo.GetDirectories method.
    Also, We could use Directory.Delete method to delete the directory in the list.

    List<Task> tasks =

    No comments

  2. Bruce (SqlWork.com) 28,036 Reputation points
    2022-06-03T16:14:20.073+00:00

    if you are trying for performance, two threads should not be deleting in the same folder, or you will slow down the deletes. if you are deleting a tree structure, start at roots.

    you say you have a list of folders, you should create a thread per folder.


  3. ZINEB BERGUEN 11 Reputation points
    2022-06-03T23:15:55.717+00:00

    thank you

    No comments