Home >Backend Development >C++ >How Can I Get the Full Path of a Running Process's Executable?
How to get the full path of an active process
In scenarios where you need to manipulate the settings of an external application and then restart it, knowing the path to the executable is crucial. However, after a process is terminated, finding its path can become a challenge because the system lacks this information.
Solution: Utilize the MainModule.FileName property
To solve this problem, you can take advantage of the MainModule.FileName property of the process object. This property provides the full path to the executable file associated with the process. The following code snippet demonstrates this approach:
<code class="language-csharp">using System.Diagnostics; var process = Process.GetCurrentProcess(); // 获取当前进程或使用您的方法 string fullPath = process.MainModule.FileName; // 获取可执行文件的完整路径</code>
Assign the value of MainModule.FileName to fullPath to access the full path of the executable file.
Note:
When your application runs as a 32-bit executable, it can only retrieve the path of the 32-bit process. To handle 64-bit processes, you need to compile and run the application as a 64-bit executable (under Project Properties → Build → Platform Target → x64).
The above is the detailed content of How Can I Get the Full Path of a Running Process's Executable?. For more information, please follow other related articles on the PHP Chinese website!