title: The process cannot access the file 'D:\test.txt'
body: <p>I have a c# code where I need to log the information in the txt file after button click. the file gets created but the write method throws an exception error : </p>
  
  <p>Write method: </p>
  
  <pre><code>public static void writelogfile(string text) 
  { 
      StreamWriter sw = logfile.AppendText(); 
      sw.WriteLine(text); 
      sw.Close(); 
  }
  </code></pre>
  
  <p>File create method: </p>
  
  <pre><code>string filename = "loging-" + DateTime.Now.ToString("yyyyMMdd-HHmmss") + ".txt"; 
  logfile = new FileInfo("D:Log\\" + filename); //for QA 
  logfile.Create(); 
  </code></pre>
  
  <p>Actual line used in the method to log the info: </p>
  
  <pre><code>writelogfile("userid: " + id);
  </code></pre>
  
  <blockquote>
    <p>The process cannot access the file 'D:test.txt' because it is being
    used by another process. at System.IO.__Error.WinIOError(Int32
    errorCode, String maybeFullPath) at System.IO.FileStream.Init(String
    path, FileMode mode, FileAccess access, Int32 rights, Boolean
    useRights, FileShare share, Int32 bufferSize, FileOptions options,
    SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy) at
    System.IO.FileStream..ctor(String path, FileMode mode, FileAccess
    access, FileShare share, Int32 bufferSize, FileOptions options, String
    msgPath, Boolean bFromProxy) at System.IO.FileStream..ctor(String
    path, FileMode mode, FileAccess access, FileShare share, Int32
    bufferSize, FileOptions options) at
    System.IO.StreamWriter..ctor(String path, Boolean append, Encoding
    encoding, Int32 bufferSize) at System.IO.StreamWriter..ctor(String
    path, Boolean append) at System.IO.FileInfo.AppendText() at
    Blocked_listupdate.VisualWebPart1.VisualWebPart1UserControl.writelogfile(String
    text) at
    Blocked_listupdate.VisualWebPart1.VisualWebPart1UserControl.&lt;>c_<em>DisplayClass1.b</em>_0()
    at Microsoft.SharePoint.SPSecurity.&lt;>c_<em>DisplayClass4.b</em>_2() at
    Microsoft.SharePoint.Utilities.SecurityContext.RunAsProcess(CodeToRunElevated
    secureCode) at
    Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(WaitCallback
    secureCode, Object param) at
    Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(CodeToRunElevated
    secureCode) at
    Blocked_listupdate.VisualWebPart1.VisualWebPart1UserControl.btnupdate_Click(Object
    sender, EventArgs e)</p>
  </blockquote>