Home >Backend Development >C++ >How Can I Get the Absolute Path of a Running Process in C#?
Get the absolute path of a running process
When modifying the settings of an external application in a C# program, it is often necessary to restart the application for the changes to be applied. However, determining the full path of a running process can be difficult if the path is not specified manually. This problem occurs because the system usually does not know the location of the EXE file after the process is created.
Solution: Use Process.MainModule.FileName property
To solve this problem, the System.Diagnostics namespace provides a solution: the Process.MainModule.FileName property. This property provides access to the absolute path to the EXE file associated with the running process.
Code example:
<code class="language-csharp">using System.Diagnostics; var process = Process.GetCurrentProcess(); // 或使用适当的方法获取目标进程 string fullPath = process.MainModule.FileName; // fullPath现在包含EXE文件的完整路径。</code>
Note:
Please note that if the code is executed in a 32-bit application, it may not be able to retrieve the path for the 64-bit application. In this case, the application needs to be compiled and run as a 64-bit application.
The above is the detailed content of How Can I Get the Absolute Path of a Running Process in C#?. For more information, please follow other related articles on the PHP Chinese website!