title: ASP.NET - C# - Handling Redundant? IOException on File.WriteAllText
body: <pre><code>private static string directory = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"LOGS\");
  private static string fileName = "log.log"
  public static void SaveFile()
  {
      File.WriteAllText(Path.Combine(directory, fileName), "MyText");
  }
  </code></pre>
  
  <p>For some reason whenever I call the above SaveFile method, File.WriteAllText throws an error stating the file is being used by another process (which I don't know what nor do I think it actually is) and Visual Studio breaks on that exception.</p>
  
  <p>However, if I tell Visual Studio to continue on with the code, the file is written to perfectly fine and everything executes normally. This is strange in and of itself, but I have tried wrapping the File.WriteAllText in a <em>try/catch</em> block just to ignore the error, but this causes errors further up the stack chain so that idea does not work.</p>
  
  <p>I have also tried a "wait-until-file-is-available" closure to handle file not being available, but that just causes an infinite loop because the file never becomes available? (see below for code)</p>
  
  <pre><code>private static string directory = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"LOGS\");
  private static string fileName = "log.log"
  
  public static void SaveFile()
  {
      bool fileSaved = false;
      while (!fileSaved)
      {
          try
          {
              File.WriteAllText(Path.Combine(directory, fileName), "MyText");
              fileSaved = true;
          }
          catch
          {
              fileSaved = false;
          }
      }
  }
  </code></pre>
  
  <p>This entire method is part of handling the logging of a critical / fatal error in an ASP.NET/C# website. I have tried some stranger solutions including manually calling GarbageCollector, calling the method via separate Thread, using Directory/FileStream/StreamWriter, etc. all without success.</p>
  
  <p>Is there a more appropriate way to handle this error or a way that it is forcefully ignored <em>JUST</em> for this one line / File.WriteAllText call?</p>