找到进程的完整路径 [英] Find the full path of process

查看:87
本文介绍了找到进程的完整路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了 c# 控制台应用程序.当我启动应用程序时(不使用 cmd).我可以看到它列在任务管理器的进程列表中,现在我需要编写另一个应用程序,我需要在其中查找以前的应用程序是否正在运行,我知道应用程序名称和路径,因此我已将管理对象搜索器查询写入获取进程列表,并且我正在使用路径将其与其可执行路径进行比较,代码如下

I have written the c# console application. when i lauch the application (not using cmd). i can see it is listed in process list in task manager ,now i need to write another application in which i need to find whether previous application is running or not ,i know the application name and path so i have written management object searcher query to get the list of process and also i am using the path to compare it with its executable path ,code is given below

            var  name="test.exe"
            var path="D:\test"
            var processCollection = new ManagementObjectSearcher("SELECT * " + "FROM Win32_Process " + "WHERE Name='" + name + "'").Get();

            if (processCollection.Count > 0)
            {
                foreach(var process in processCollection)
                {
                    var executablePath=process["ExecutablePath"];
                    if(executablePath.equals(path))
                    {
                        return true;
                    }
                }
            }

但可执行路径始终为空.

but executable path is always null.

如何获取其可执行路径?.

how to get its executable path?.

我不仅可以使用进程名称,因为我在我的应用程序中使用了通用名称,如 startserver 和 stopserver.所以我需要确保它的可执行路径.

I can not only use process name because i am using common name like startserver and stopserver to my application. so i need to ensure it executable path.

推荐答案

有一个更简单的方法,导入System.Diagnostics然后编写如下代码,

there is a easier way, import System.Diagnostics then write following code,

public bool ProcessIsRun()
{
    Process[] proc = Process.GetProcesses();
    Return proc.Any(m => m.ProcessName.Contains("Your process name") && m.Modules[0].FileName == "Your File Path" );
}

这篇关于找到进程的完整路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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