Home >Backend Development >C++ >How Can I Get the File Path of a Running Process in C#?

How Can I Get the File Path of a Running Process in C#?

Patricia Arquette
Patricia ArquetteOriginal
2025-01-15 15:27:44695browse

How Can I Get the File Path of a Running Process in C#?

How to get the file path of a running process in C#

When using a C# application to modify the settings of an external application, you may need to restart the external application to apply the changes. However, this poses challenges due to the lack of information about the location of the exe file, which requires finding the process path after terminating the process.

Solution: Get the file path of the running process

To solve this problem, the following code snippet demonstrates how to retrieve the full path of the currently running process:

<code class="language-csharp">using System.Diagnostics;
var process = Process.GetCurrentProcess(); // 或者使用适当的方法获取所需的进程
string fullPath = process.MainModule.FileName;
// fullPath 现在包含exe文件的路径。</code>

Important Tips:

If the code is executed in a 32-bit application, it will not be able to access the path of the 64-bit application. To resolve this issue, ensure that both the application and code compile and run as 64-bit (in Visual Studio, this can be set under Project Properties → Build → Platform Target → x64).

The above is the detailed content of How Can I Get the File Path of a Running Process in C#?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn