Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a Mutex throws a DirectoryNotFoundException

Tags:

.net

mutex

I'm trying to create a named mutex, but when I call the constructor I get a DirectoryNotFoundException! Why is a mutex trying to access the filesystem, and how do I know what is a valid path? Is there any particular directory the mutex should be placed in, and how does that correspond to the name?

EDIT: I'm using the Mutex(bool, string) overload, and the exception is:

System.IO.DirectoryNotFoundException: Could not find a part of the path '<mutex name>'.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.Threading.Mutex.<>c__DisplayClass3.<.ctor>b__0(Object userData)
at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
at System.Threading.Mutex..ctor(Boolean initiallyOwned, String name, Boolean& createdNew, MutexSecurity mutexSecurity)
at System.Threading.Mutex..ctor(Boolean initiallyOwned, String name)
like image 631
thecoop Avatar asked Nov 30 '10 12:11

thecoop


1 Answers

Ah, found what the problem was. My mutex name had \ in it, which windows was interpreting as a path character. Running:

mutexName = mutexName.Replace(Path.DirectorySeparatorChar, '_');

fixes the problem

like image 70
thecoop Avatar answered Sep 28 '22 14:09

thecoop