Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Application.ExecutablePath returns dll instead of exe #2801

Merged
merged 1 commit into from
Feb 10, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Member

@Tanya-Solyanik Tanya-Solyanik Feb 4, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like after the change, file path is not formatted, instead, according to docs, we return what was specified when the module was loaded.

The string returned will use the same format that was specified when the module was loaded. Therefore, the path can be a long or short file name, and can use the prefix "\?". For more information, see Naming a File.

Also if the app was started using only only the file name and GetModuleFileName returns that short name, then Path.GetFullPath would resolve the full path based on the current working directory, which might not be the same as the app was started from. See remarks here.

[EDIT] fix formatting

Copy link
Contributor

@weltkante weltkante Feb 4, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure GetModuleFileName can return a relative file name? I was under the impression that only the format (long/short/escaped) could differ, but that it always is an absolute path. (Doc says "Retrieves the fully qualified path ..." as its first line and then notes later that the format can differ, but I don't think it retracts the fact that its fully qualified.)

;
}
else
{
s_executablePath = codeBase.ToString();
}
}
StringBuilder sb = UnsafeNativeMethods.GetModuleFileNameLongPath(NativeMethods.NullHandleRef);
s_executablePath = Path.GetFullPath(sb.ToString());
}

return s_executablePath;
Expand Down