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

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

Mary-Kate Olsen
Release: 2025-01-15 15:06:44
Original
863 people have browsed it

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>
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template