Skip to content

Commit

Permalink
fix: Application.ExecutablePath returns dll instead of exe
Browse files Browse the repository at this point in the history
In .NET artifacts are DLLs even for executable projects. With some automagic
they get bundled into executables.
However `Assembly.GetEntryAssembly()` always returns the dll instead of the exe.

Following the guidance from the Runtime team retrieve the path to the
executable via `GetModuleFileNameW` call.

Resolves dotnet#1143
  • Loading branch information
RussKie committed Feb 4, 2020
1 parent 106bd36 commit 71f82bb
Showing 1 changed file with 2 additions and 20 deletions.
22 changes: 2 additions & 20 deletions src/System.Windows.Forms/src/System/Windows/Forms/Application.cs
Original file line number Diff line number Diff line change
Expand Up @@ -288,26 +288,8 @@ public static string ExecutablePath
{
if (s_executablePath == null)
{
Assembly asm = Assembly.GetEntryAssembly();
if (asm == null)
{
StringBuilder sb = UnsafeNativeMethods.GetModuleFileNameLongPath(NativeMethods.NullHandleRef);
s_executablePath = Path.GetFullPath(sb.ToString());
}
else
{
string cb = asm.CodeBase;
Uri codeBase = new Uri(cb);
if (codeBase.IsFile)
{
s_executablePath = codeBase.LocalPath + Uri.UnescapeDataString(codeBase.Fragment);
;
}
else
{
s_executablePath = codeBase.ToString();
}
}
StringBuilder sb = UnsafeNativeMethods.GetModuleFileNameLongPath(NativeMethods.NullHandleRef);
s_executablePath = Path.GetFullPath(sb.ToString());
}

return s_executablePath;
Expand Down

0 comments on commit 71f82bb

Please sign in to comment.