Skip to content

Commit

Permalink
fix: Application.ExecutablePath returns dll instead of exe (#2801)
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 #1143

(cherry picked from commit 2af3af9)
  • Loading branch information
RussKie committed Feb 11, 2020
1 parent b3a34e6 commit abd221b
Showing 1 changed file with 3 additions and 21 deletions.
24 changes: 3 additions & 21 deletions src/System.Windows.Forms/src/System/Windows/Forms/Application.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Licensed to the .NET Foundation under one or more agreements.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

Expand Down Expand Up @@ -340,26 +340,8 @@ public static string ExecutablePath
{
if (executablePath == null)
{
Assembly asm = Assembly.GetEntryAssembly();
if (asm == null)
{
StringBuilder sb = UnsafeNativeMethods.GetModuleFileNameLongPath(NativeMethods.NullHandleRef);
executablePath = Path.GetFullPath(sb.ToString());
}
else
{
string cb = asm.CodeBase;
Uri codeBase = new Uri(cb);
if (codeBase.IsFile)
{
executablePath = codeBase.LocalPath + Uri.UnescapeDataString(codeBase.Fragment);
;
}
else
{
executablePath = codeBase.ToString();
}
}
StringBuilder sb = UnsafeNativeMethods.GetModuleFileNameLongPath(NativeMethods.NullHandleRef);
executablePath = Path.GetFullPath(sb.ToString());
}

return executablePath;
Expand Down

0 comments on commit abd221b

Please sign in to comment.