title: Append to the same text file in c#
body: <p>I need to append to a text file with the same code running in multiple instances. In the try block I open the file, write one record and close.</p>
  
  <pre><code>        lfilExceptionLogFileStream = new FileStream(istrExceptionInfoFullFileName, FileMode.Append, FileAccess.Write, FileShare.Write);
          lfilExceptionLog = new StreamWriter(lfilExceptionLogFileStream);
  </code></pre>
  
  <p>Should there be an issue, in the catch block I pause for 1 sec <code>System.Threading.Thread.Sleep(1000);</code> and call the same function. I retry 99 times.</p>
  
  <p>With <code>FileShare.Write</code> when I test concurrently 3 times - data is lost. <code>FileShare.None</code> - locks the file for a long time</p>
  
  <p>I have tried quite a bit. If there is an easy way to find if the file is locked I can wait until it is locked. I have tried using the lock object - no luck</p>
  
  <p>Appreciate if somebody can provide a workable solution.</p>
  
  <p>Ravi</p>