title: File locking - read then write whilst locked
body: <p>I need to be able to open a file to read it, maintaining a lock that denies other instances of the same application write access until I have written the amended file back to disk. The file is in a shared location on a network, and the app instances can be on any machine on the network.</p>
  
  <p>I have tried using a <code>FileStream</code> with <code>FileAccess.ReadWrite</code> and <code>FileShare.Read</code>, with a <code>Streamreader</code> to read and then a <code>StreamWriter</code> (on the same <code>FileStream</code>) to write, but this corrupts the file. Other permutations of the <code>FileAccess</code> and <code>FileShare</code> don't seem to solve my basic problem either.</p>
  
  <p>So I tried closing the <code>StreamReader</code> before opening the <code>StreamWriter</code>, but that changes the <code>CanRead</code> and <code>CanWrite</code> properties of the <code>FileStream</code>, so I still can't write.</p>
  
  <p>Clearly I am taking the wrong approach, so can someone tell me how I should be approaching this? It seems a common enough thing to want to do - edit a file and block write access until the edited file is saved.</p>