title: The process cannot access the file because it is being used by another process c# asp.net
body: <p>I am simply trying to create a file using c# and writing in it. Here is my code:</p>
  
  <pre><code>public void AddLog (string message, string fn_name)
  {
      string path = @"E:\" + fn_name +".txt";
      if (!File.Exists(path))
      {
          File.Create(path);
          File.Delete(path);
          TextWriter tw = new StreamWriter(path);
          tw.WriteLine("" + message +"");
          tw.Close();
      }
      else if (File.Exists(path))
      {
          TextWriter tw = new StreamWriter(path,true);
          tw.WriteLine("" + message + "");
          tw.Close();
      }
  }
  </code></pre>
  
  <p>The problem is it always gives me this exception: </p>
  
  <blockquote>
    <p>{"The process cannot access the file 'E:\CheckForFriends.txt' because
    it is being used by another process."}</p>
  </blockquote>
  
  <p>at this line: <code>File.Delete(path);</code>
  and even if I removed this line it gives me the same exception message at the line: <code>File.Create(path);</code></p>
  
  <p>So, anyone have an idea what is wrong in my code?</p>