过得去名正在运行的进程的路径 [英] Getting a path of a running process by name

查看:80
本文介绍了过得去名正在运行的进程的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能获得通过名称正在运行的进程的路径?例如,我知道有一个名为记事本正在运行的进程,我要得到它的路径。如何获得,无需通过其他所有进程循环路径?



不是这样!



 使用System.Diagnostics程序; 

的foreach(过程PPATH在Process.GetProcesses())
{
如果(PPath.ProcessName.ToString()==记事本)
{
串FULLPATH = PPath.MainModule.FileName;
Console.WriteLine(完整路径);
}
}


解决方案

尝试像这样的方法,它使用 GetProcessesByName 方法

 公共字符串GetProcessPath(字符串名称)
{
过程[] =流程Process.GetProcessesByName(名);

如果(processes.Length大于0)
{
返回过程[0] .MainModule.FileName;
}
,否则
{
返回的String.Empty;记住
}
}

请虽然,多个进程可以有相同的名字,所以你仍然可能需要做一些挖掘。我只是一直在这里返回第一个路径。


How can I get a path of a running process by name? For example, I know there is a process named "notepad" running, and I want to get the path of it. How to get the path without looping through all other processes?

Not this way!

using System.Diagnostics;

foreach (Process PPath in Process.GetProcesses())
{
    if (PPath.ProcessName.ToString() == "notepad")
    {
        string fullpath = PPath.MainModule.FileName;
        Console.WriteLine(fullpath);
    }
}

解决方案

Try something like this method, which uses the GetProcessesByName method:

public string GetProcessPath(string name)
{
    Process[] processes = Process.GetProcessesByName(name);

    if (processes.Length > 0)
    {
        return processes[0].MainModule.FileName;
    }
    else
    {
        return string.Empty;
    }
}

Keep in mind though, that multiple processes can have the same name, so you still might need to do some digging. I'm just always returning the first one's path here.

这篇关于过得去名正在运行的进程的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆