From 71f82bb8f1262845bd83ff7ed889ed549907e32c Mon Sep 17 00:00:00 2001 From: Igor Velikorossov Date: Tue, 4 Feb 2020 14:20:34 +1100 Subject: [PATCH] fix: Application.ExecutablePath returns dll instead of exe 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 --- .../src/System/Windows/Forms/Application.cs | 22 ++----------------- 1 file changed, 2 insertions(+), 20 deletions(-) diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/Application.cs b/src/System.Windows.Forms/src/System/Windows/Forms/Application.cs index 99c078fb58b..a98e6f0e63c 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/Application.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/Application.cs @@ -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;