Creating a Mutex throws a DirectoryNotFoundExcepti

2019-06-14 23:50发布

问题:

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)

回答1:

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



标签: .net mutex