Deleting Files With Events

While working on the dotNetTips Dev Cleaner utility, I wanted to make the deletion of files even faster. While writing this utility I found and worked on speed issues, almost all relating to updating the user interface. So to decouple the deleting from the UI, I decided to add a new feature to the dotNetTips.Utility open source project.

Processor Class

I added a new class in the dotNetTips.Utility.IO namespace called Processor. The purpose of this class is to copy, move and delete files while firing events that can be used to update the UI. Unlike other methods I have used in other frameworks if an exception occurs, it fires an event and keeps processing.

First I created the event,


  1. public event EventHandler<ProgressEventArgs> Processed;  
  2.           
  3. protected virtual void OnProcessed(ProgressEventArgs e)  
  4. {  
  5.     EventHandler<ProgressEventArgs> processedEvent = this.ProcessedEvent;  
  6.     if (processedEvent != null)  
  7.     {  
  8.         processedEvent(this, e);  
  9.     }  
  10. }   

  11. //The event above is called by the code below:  
  12.    
  13.         public int DeleteFiles(IEnumerable<FileInfo> files)  
  14.         {  
  15.             Encapsulation.TryValidateParam(files, "files""");  
  16.             int result = 0;  
  17.             IEnumerator<FileInfo> enumerator;  
  18.             try  
  19.             {  
  20.                 enumerator = files.AsParallel<FileInfo>().GetEnumerator();  
  21.                 while (enumerator.MoveNext())  
  22.                 {  
  23.                     FileInfo current = enumerator.Current;  
  24.                     if (current.Exists)  
  25.                     {  
  26.                         try  
  27.                         {  
  28.                             current.Delete();  
  29.                             result++;  
  30.                             ProgressEventArgs e = new ProgressEventArgs();  
  31.                             e.Name = current.FullName;  
  32.                             e.ProgressState = ProgressState.Deleted;  
  33.                             e.Size = current.Length;  
  34.                             this.OnProcessed(e);  
  35.                             continue;  
  36.                         }  
  37.                         catch (Exception ex)  
  38.                         {  
  39.                             ProjectData.SetProjectError(ex);  
  40.                             ProgressEventArgs e1 = new ProgressEventArgs();  
  41.                             e1.Name = current.FullName;  
  42.                             e1.ProgressState = ProgressState.Error;  
  43.                             e1.Size = current.Length;  
  44.                             e1.Message = ex.Message;  
  45.                             this.OnProcessed(e1);  
  46.                             ProjectData.ClearProjectError();  
  47.                             continue;  
  48.                         }  
  49.                     }  
  50.   
  51.                     ProgressEventArgs e2 = new ProgressEventArgs();  
  52.                     e2.Name = current.FullName;  
  53.                     e2.ProgressState = ProgressState.Error;  
  54.                     e2.Size = current.Length;  
  55.                     e2.Message = Resources.FileNotFound;  
  56.                     this.OnProcessed(e2);  
  57.                 }  
  58.             }  
  59.             finally  
  60.             {  
  61.                 if (enumerator != null)  
  62.                 {  
  63.                     enumerator.Dispose();  
  64.                 }  
  65.             }  
  66.   
  67.             return result;  
  68.         }  
This new class helped my utility go from deleting 1K files per second to up to around 2K per second! 
Ebook Download
View all
Learn
View all