Keep your temp folder clean

By Mirek on (tags: Process, Task, Temp, categories: code)

Temporary folders in your system are used to store temporary files obviously. Unfortunately those temporary files, very often, stays there for ever and makes your hard drive wasted.

Indeed many applications uses temp folder to store auxiliary files but do not clear it causing temp folder grow and grow. In this post I would like to present my short code snippet to keep temp folder clean in a quite (I think) common use case. The file is normally stored in database as byte array, but we want it to by executed by a default registered application. For that we need to have it saved on hard drive and be accessible for that application. So we first get the file from database, store it on hard drive (in temporary folder), then execute it and after all we want it to be deleted.

 

   1: public void ExecuteFile(byte[] data, string fileName)
   2: {
   3:     try
   4:     {
   5:         string auxfileName = Path.Combine(Path.GetTempPath(), fileName);
   6:         System.IO.File.WriteAllBytes(auxfileName, data);
   7:  
   8:         Task.Factory.StartNew(() =>
   9:             {
  10:                 Process proc = new Process();
  11:                 proc.StartInfo.FileName = auxfileName;
  12:                 proc.Start();
  13:                 proc.WaitForExit();
  14:             })
  15:             .ContinueWith((task) =>
  16:             {
  17:                 File.Delete(auxfileName);
  18:             });
  19:     }
  20:     catch (Exception ex)
  21:     {
  22:         throw new Exception("Error occured while opening file from byte array", ex);
  23:     }
  24: }
 
As you can see we make use of System.Diagnostics.Process to run a file with its default assigned application, so we leave the rest to operating system to handle. Especially we use the WaitForExit() method which basically holds the execution of current thread until the associated process finish. After that we define an action that has to be performed after our task is completed. In those action (line 17) we delete the temporary file and clear our temp directory.
That’s it. Simple and effective. What we all like Puszczam oczko
 
UPDATE:
To properly handle exceptions that are thrown from the task we should await it, so it will look as follows
 
   1: try
   2: {                
   3:  
   4:     string auxfileName = Path.Combine(Path.GetTempPath(), fileName);
   5:  
   6:     await Task.Run(() =>
   7:     {
   8:         System.IO.File.WriteAllBytes(auxfileName, data);
   9:         Process proc = new Process();
  10:         proc.StartInfo.FileName = auxfileName;
  11:         proc.Start();
  12:         proc.WaitForExit();
  13:     })
  14:     .ContinueWith((task) =>
  15:     {
  16:         File.Delete(auxfileName);
  17:     });
  18: }
  19: catch (Exception ex)
  20: {
  21:     Log.Error("Error occured while opening file from byte array", ex);
  22:     throw;
  23: }
By the way we also put the WriteAllBytes  call into a asynchronous part, since it can take some time to accomplish for big files.