Creating a Mutex throws a DirectoryNotFoundException

泪湿孤枕 提交于 2019-12-08 14:46:55

问题


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



来源:https://stackoverflow.com/questions/4313756/creating-a-mutex-throws-a-directorynotfoundexception

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!