title: Deleting files in use
body: <p>I created a simple program to delete temporary files in C# (for fun, not a major project) and am running into locked files (in use) issues.  How do you normally either exclude those files? For reference I am receiving the error:</p>
  
  <p>The process cannot access the file 'ExchangePerflog_8484fa31c65c7a31cfcccd43.dat' because it is being used by another process.</p>
  
  <p>Code:</p>
  
  <pre><code>static void Main(string[] args)
      {
          string folderPath = string.Empty;
          folderPath = System.Environment.GetEnvironmentVariable("temp");
          deleteFilesInDirectory(folderPath);
      }
  
      public static void deleteFilesInDirectory(string folderPath) 
      {
  
          try
          {
              var dir = new DirectoryInfo(folderPath);
              dir.Attributes = dir.Attributes &amp; ~FileAttributes.ReadOnly;
              dir.Delete(true);
              MessageBox.Show(folderPath + " has been cleaned.");
          }
          catch (System.IO.IOException ex)
          {
              MessageBox.Show(ex.Message); 
              return;
  
          } 
      }     
  </code></pre>