title: Detecting whether a file is locked by another process (or indeed the same process)
body: <p>This is how I do it at the moment. I try to open the file with the FileShare set to none. So I want exclusive accesss to the file. If I can't get that then its a good bet somebody else has the file locked.</p>
  
  <p>There's got to be a better and faster way. Any ideas?</p>
  
  <pre><code>            try
              {
                  using (FileStream fs = File.Open(GetLockFilename(), FileMode.Open, FileAccess.ReadWrite, FileShare.None))
                  {
                      fs.Close();
                  }
                  // The file is not locked
              }
              catch (Exception)
              {
                  // The file is locked
              }
  </code></pre>