Skip to content

Use Environment.ProcessPath instead of Process.GetCurrentProcess().Pr… #10998

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ internal static void LogStartHWND(IntPtr hwnd, string fromWhere)
{
string msg = String.Format("BEGIN: {0:X} -- Setting DWP, process = {1} ({2}) {3}",
hwnd,
System.Diagnostics.Process.GetCurrentProcess().ProcessName,
Path.GetFileNameWithoutExtension(Environment.ProcessPath),
Copy link
Member

@lindexi lindexi Jul 22, 2025

Choose a reason for hiding this comment

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

Environment.ProcessPath will call the Kernel32.GetModuleFileNameW, see https://learn.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-getmodulefilenamew

System.Diagnostics.Process.GetCurrentProcess().ProcessName will call Kernel32.QueryFullProcessImageNameW, see https://learn.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-getmodulefilenamew

I do not think this is the same behavior. But I believe it is the same behavior in the vast majority of cases.

Copy link
Author

@NeverMorewd NeverMorewd Jul 23, 2025

Choose a reason for hiding this comment

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

Process.GetCurrentProcess() without using will left a Process instance after each calling.
In certain scenarios with high call volume, this pr will make sense.

Copy link
Contributor

Choose a reason for hiding this comment

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

But I believe it is the same behavior in the vast majority of cases.

For WPF both .NET APIs return the same result. There is no difference. Only for other .NET scenarios (not WPF related: framework dependend when running from dotnet.exe, browser etc) there is a difference between Environment.ProcessPath and ProcessName. Yes, they call different winapis but for WPF the result is the same in all cases.

Copy link
Contributor

Choose a reason for hiding this comment

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

One difference is when the file is renamed while the process is running. But the discussion is moot as per the PR discussion.

fromWhere,
System.Environment.NewLine);

Expand All @@ -214,7 +214,7 @@ internal static void LogFinishHWND(IntPtr hwnd, string fromWhere)
{
string msg = String.Format("END: {0:X} -- Setting DWP, process = {1} ({2}) {3}",
hwnd,
System.Diagnostics.Process.GetCurrentProcess().ProcessName,
Path.GetFileNameWithoutExtension(Environment.ProcessPath),
fromWhere,
System.Environment.NewLine);

Expand Down