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:
System.IO.IOException: The requested operation cannot be performed on a file with a user-mapped section open.
I realized that I am calling the File.Exists()
method before obtaining a lock on the mutex, is it possible that this could be the cause?
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.
Rough example of current 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;
}
}
EDIT: 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.