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

Conversation

NeverMorewd
Copy link

@NeverMorewd NeverMorewd commented Jul 22, 2025

Fixes #

Main PR

Description

Replace Process.GetCurrentProcess().ProcessName with Path.GetFileNameWithoutExtension(Environment.ProcessPath) for better performance and cleaner code.

Customer Impact

  • Reduced system call overhead
  • No temporary Process object creation

Regression

Testing

Risk

Microsoft Reviewers: Open in CodeFlow

@NeverMorewd NeverMorewd requested a review from a team as a code owner July 22, 2025 03:42
@dotnet-policy-service dotnet-policy-service bot added PR metadata: Label to tag PRs, to facilitate with triage Community Contribution A label for all community Contributions labels Jul 22, 2025
Copy link

codecov bot commented Jul 22, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 13.42425%. Comparing base (725fc8a) to head (eec8f43).

Additional details and impacted files
@@                 Coverage Diff                 @@
##                main      #10998         +/-   ##
===================================================
- Coverage   13.42545%   13.42425%   -0.00120%     
===================================================
  Files           3319        3319                 
  Lines         664894      664894                 
  Branches       74674       74674                 
===================================================
- Hits           89265       89257          -8     
- Misses        573084      573092          +8     
  Partials        2545        2545                 
Flag Coverage Δ
Debug 13.42425% <ø> (-0.00120%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@@ -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.

@miloush
Copy link
Contributor

miloush commented Jul 22, 2025

I agree this is a compatibility risk.

@lindexi
Copy link
Member

lindexi commented Jul 22, 2025

@miloush This is the edge module code, and it only effect the log message. So that I think it is ok.

@miloush
Copy link
Contributor

miloush commented Jul 22, 2025

Which should be discussed in the issue description, together with how it was tested. You have to have a special build with LOGGING set, so the impact of this change is questionable.

@lindexi
Copy link
Member

lindexi commented Jul 22, 2025

@miloush I think it is the debug code.

@jizc
Copy link
Contributor

jizc commented Jul 22, 2025

Just want to point out that the contents of the Log method that msg is sent to has been commented out.

private static void Log(string msg)
{
//DbgUserBreakPoint();
/*
byte[] msgBytes = System.Text.Encoding.ASCII.GetBytes(msg);
System.IO.FileStream fs = System.IO.File.Open("c:\\dwplog.txt", System.IO.FileMode.Append, System.IO.FileAccess.Write, System.IO.FileShare.ReadWrite);
fs.Write(msgBytes, 0, msgBytes.Length);
fs.Flush();
fs.Close();
*/
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Community Contribution A label for all community Contributions PR metadata: Label to tag PRs, to facilitate with triage
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants