Getting a path of a running process by name

前端 未结 3 1197
花落未央
花落未央 2021-01-12 04:22

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 wi

3条回答
  •  渐次进展
    2021-01-12 04:47

    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.

提交回复
热议问题