如何获得运行过程的完整路径? [英] How to get the full path of running process?
问题描述
我有一个正在更改其他应用程序设置的应用程序(这是一个通过双击运行的简单C#应用程序(无需安装))。
I am having a application that is changing some settings of other application (it is a simple C# application that run by double clicking (no setup required)).
更改设置后,我需要重新启动另一个应用程序,以便它反映更改后的设置。
After changing the settings I need to restart the other application so that it reflects the changed settings.
这样做,我必须终止正在运行的进程并重新启动该进程,但是问题是在终止后我找不到该进程。 (原因是系统不知道exe文件在哪里。)
So to do, I have to kill the running process and start the process again, But the problem is after killing I am not able to find the process. (Reason is system do not know where the exe file is..)
有没有办法找出正在运行的进程或exe的路径,如果有的话
我不想手动给出路径,即如果它正在运行,请获取路径,杀死该过程,然后再重新开始...稍后,我将使用System.Diagnostics处理
I do not want to give path manually, i.e. if it is running get the path, kill the proccess and start again else .... I will handle later
推荐答案
using System.Diagnostics;
var process = Process.GetCurrentProcess(); // Or whatever method you are using
string fullPath = process.MainModule.FileName;
//fullPath has the path to exe.
此API有一个陷阱,如果您在32位应用程序中运行此代码,则不能访问64位应用程序路径,因此您必须将应用程序编译为64位应用程序并运行(项目属性→构建→平台目标→x64)。
There is one catch with this API, if you are running this code in 32 bit application, you'll not be able to access 64-bit application paths, so you'd have to compile and run you app as 64-bit application (Project Properties → Build → Platform Target → x64).
这篇关于如何获得运行过程的完整路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!