title: File.Exists() and File Access Synchronization across processes
body: <p>I have a couple applications running in separate processes that each write to a central xml doc on the harddrive.  I am using a named mutex to synchronize access to the file - but get the following exception often:</p>
  
  <pre><code>System.IO.IOException: The requested operation cannot be performed on a file with a user-mapped section open.
  </code></pre>
  
  <p>I realized that I am calling the <code>File.Exists()</code> method before obtaining a lock on the mutex, is it possible that this could be the cause?</p>
  
  <p>More simply, can anyone confirm if that method does a Read on the file to check if it exists?  Im wondering if I need to lock the mutex before checking if the file is present.
  I did verify that there is no AV software running, as that seemed to be common with this exception.</p>
  
  <p>Rough example of current code:</p>
  
  <pre><code>private bool UpdateFile(string Text)
  {
    if (!File.Exists(@"C:\FileName.xml"))
      return false;
    else
    {
      using (Mutex mutex = new Mutex(false, "ExampleMutexName"))
      {
        mutex.WaitOne(); 
        //Write to the file
        mutex.ReleaseMutex();
      }
      return true;
    }
  }
  </code></pre>
  
  <p><strong>EDIT:</strong> I was not using a global mutex because everything runs inside the same user account - so I believed a local mutex would be fine.  However, one of the processes I start is with impersonation - which is why I SHOULD have used a global mutex.  My mistake was just thinking that everything runs with in the same user account and would be fine.</p>