Mutex throwing DirectoryNotFound exception

The following line:

Mutex mutex = new Mutex( true , GetYourBackupProgram._ProgramFolder, out mutexCreated );

throws System.IO.DirectoryNotFoundException.

Does anybody know why

The full exception is below. According to the ms documentation this exception is not thrown although IOException can be. There is no indication (that I saw) in the document that it checks the Mutex name for being a directory. The second odd thing is that the directory DOES exist, as that is where the program is running from. The workaround is to replace the ":" and "\" characters in the path of the path with something else.

Can some one tell me if this is a bug in

  1. MS code
  2. MS documentation
  3. My brain

Regards - Paul


System.IO.DirectoryNotFoundException was unhandled
Message="Could not find a part of the path 'C:\\code\\c#\\GetYourBackUp\\bin\\Debug'."
Source="mscorlib"
StackTrace:
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, Boolean& createdNew)
at GetYourBackup.GetYourBackupProgram.Main(String[] argumentz) in C:\code\c#\GetYourBackUp\Configuration\GetYourBackupProgram.cs:line 140
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()



Answer this question

Mutex throwing DirectoryNotFound exception

  • Mapa3matuk

    MS documentation says that IOException can be thrown and DirectoryNotFoundException is derived from IOException so the documentation is correct. Fell free to say that it should be more accurate by saying exactly what IOException is thrown but I'm not sure it's really needed in this case.

    MS code is also OK. The name you use for your mutex includes \ so parts like C:, code, c#, GetYourBackUp, bin, Debug will be considered directories but here's where the MS doc bug is. They assume that all people know that named kernel objects like mutexes, semaphores, events are "created" in something that is called an Object Directory and that \ character actually matters as a path delimiter. Of course where will be no object directory called bin or GetYourBackUp and nor you can create one in an easy way.

    For more details take a look at the Win32 function CreateMutex:

    http://msdn.microsoft.com/library/default.asp url=/library/en-us/dllproc/base/createmutex.asp

    You'll see there what the mutex name be.

    The usual approach for this case is to replace \ with _ (: also works, it's not considered a special character).

    Also since you're working with named mutexes you my find useful a little tool available at www.sysinternals.com site called WinObj. That will give you an insight about kernel object namespace and where are these named objects created (while your program is running you should find your mutex under \BaseNamedObject dir).


  • Peter Smith

    Can you show us how the GetYourBackupProgram._ProgramFolder implementation looks like.
    The Exception is not (can't)  thrown by the Mutex class, but probably by the property getter.
     
    Willy.
     

    The following line:

    Mutex mutex = new Mutex( true , GetYourBackupProgram._ProgramFolder, out mutexCreated );

    throws System.IO.DirectoryNotFoundException.

    Does anybody know why

    The full exception is below. According to the ms documentation this exception is not thrown although IOException can be. There is no indication (that I saw) in the document that it checks the Mutex name for being a directory. The second odd thing is that the directory DOES exist, as that is where the program is running from. The workaround is to replace the ":" and "\" characters in the path of the path with something else.

    Can some one tell me if this is a bug in

    1. MS code
    2. MS documentation
    3. My brain

    Regards - Paul


    System.IO.DirectoryNotFoundException was unhandled
    Message="Could not find a part of the path 'C:\\code\\c#\\GetYourBackUp\\bin\\Debug'."
    Source="mscorlib"
    StackTrace:
    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, Boolean& createdNew)
    at GetYourBackup.GetYourBackupProgram.Main(String[] argumentz) in C:\code\c#\GetYourBackUp\Configuration\GetYourBackupProgram.cs:line 140
    at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
    at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
    at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
    at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
    at System.Threading.ThreadHelper.ThreadStart()


  • jh0483

    To: NNTP user

    Please be aware that I do not understand mutex(e )s, so what I have here my not be a a decent implementation. However, it appears to work as I want. The current code looks like this:

    /*

    * Check if the application is already running with the same configuration.xml.

    * You can run as many copies as you want but they must each use a different

    * configuration file.

    */

    bool mutexCreated;

    string mutexName = ConfigurationFile.Replace( ":" , "_" );

    mutexName = mutexName.Replace( "\\" , "/" );

    Mutex mutex = new Mutex( true , mutexName , out mutexCreated );

    if (!mutexCreated)

    {

    Util.MessageOk( _ProgramName + " is already running. Click the icon in the System Tray to bring it back to life" );

    //GetYourBackupForm.SendMessageToOtherForm( );

    return;

    }

    GC.KeepAlive( mutex );


  • walkingboy

    Thank you for your considerate and considered reply. Once again I have failed to find my way to the correct part of the documentation. Isaac Newton once again springs to mind:

    I do not know what I may appear to the world; but to myself I seem to have been only like a boy playing on the sea-shore, and diverting myself in now and then finding a smoother pebble or a prettier shell than ordinary, whilst the great ocean of truth lay all undiscovered before me.


  • Mutex throwing DirectoryNotFound exception