I discovered an interesting bug where the Application.ExecutablePath property returns the wrong path.  I haven’t debugged this thoroughly to determine why the problem occurs, but I thought I would share my experience.  Here’s my setup:

  • Visual Studio 2008
  • Solution with a DLL, and a WinForms EXE that references the DLL

When I run the application in the Visual Studio debugger, ExecutablePath returns the DLL path instead of the EXE path!  Note that if I run the EXE directly from Windows Explorer, ExecutablePath returns the EXE path correctly.

Because it’s critical to run our project in the debugger, I wrapped the ExecutablePath property with the following hack so that it works correctly both in and out of the debugger.  Note that this hack works because the DLL and EXE have the same path and name, but differ only by extension.

public string MyExecutablePath
{
    get
    {
        string path = Application.ExecutablePath;
        string extension = Path.GetExtension( path ).ToLower();
        if (String.Equals( extension, ".dll" ))
        {
            string folder = Path.GetDirectoryName( path );
            string fileName = Path.GetFileNameWithoutExtension( path );
            fileName = String.Concat( fileName, ".exe" );
            path = Path.Combine( folder, fileName );
        }
        return path;
    }
}