Home >Backend Development >C++ >How Can I Get the Execution Path of a Running Process in C#?
Get the execution path of the running process
In scenarios where a C# application needs to modify the settings of another application and restart to reflect those changes, determining the execution path of the target process is crucial. If the target program is launched by double-clicking it, the operating system may not automatically recognize the path to its executable file (.exe).
To solve this problem, Microsoft's .NET Framework provides an API that allows you to get the full path of the running process:
<code class="language-csharp">using System.Diagnostics; var process = Process.GetCurrentProcess(); // 或您的目标进程 string fullPath = process.MainModule.FileName;</code>
This code assigns the execution path of the specified process to the fullPath variable.
Important Tips:
Please note that if the code is running in a 32-bit application, the path to the 64-bit application will not be accessible. To resolve this issue, in the Visual Studio project settings, navigate to Project Properties → Build → Target Platform → x64 to compile and run your application as a 64-bit application.
The above is the detailed content of How Can I Get the Execution Path of a Running Process in C#?. For more information, please follow other related articles on the PHP Chinese website!