找到进程的完整路径

编程入门 行业动态 更新时间:2024-10-24 12:33:35
本文介绍了找到进程的完整路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我已经编写了 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" ); }

更多推荐

找到进程的完整路径

本文发布于:2023-11-30 05:34:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1648897.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:路径   进程   完整

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!