C# WMI 进程差异化?

发布于 2024-08-28 06:54:18 字数 242 浏览 5 评论 0原文

场景

我有一个使用 WMI 返回进程列表的方法。 如果我有 3 个正在运行的进程(全部都是 C# 应用程序) - 并且它们都具有相同的进程名称但不同的命令行参数,如果我想启动它们或终止它们,我如何区分它们!?

据我所知

,我在物理上无法区分它们,至少在不使用句柄的情况下无法区分,但这并不能告诉我其中哪一个被终止,因为其他人仍然会以相同的名称坐在那里。 .......

....真的很难过,帮助不胜感激!

Scenario

I have a method that returns a list of processes using WMI.
If I have 3 processes running (all of which are C# applications) - and they all have THE SAME PROCESS NAME but different command line arguments, how can I differentiate between them If I want to start them or terminate them!?

Thoughts

As far as I can see, I physically cannot differentiate between them, at least not without having to use the Handle, but that doesn't tell me which one of them got terminated because the others will still sit there with the same name........

....really stumped, help greatly appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尘世孤行 2024-09-04 06:54:18

使用将进程 ID 作为输出参数的技术来创建进程。例如

然后您可以使用该值来真正了解稍后要杀死哪个版本的进程。例如

(请注意,如果该过程在您之前停止杀死它,操作系统可以将相同的进程ID分配给一个新进程,所以当然你需要仔细检查你是否杀死了正确的进程,例如也检查进程名称)

Create the process using a technique that gives you the process ID as an out parameter. E.g.

Then you can use that value to truly know which version of the process you want to kill later. E.g.

  • WMI: Get Win32_Process instance matching ProcessId, call Terminate()
  • .NET: Get Process instance using GetProcessById, call Kill and then WaitForExit

(Note that if the process stops before you kill it, the OS can assign that same process ID to a new process, so of course you'll want to double check that you're killing the right one, e.g. check the process name too)

女皇必胜 2024-09-04 06:54:18

WMI Win32_ProcessObject 有一个 CommandLine 属性,如果您知道该属性可以区分实例,则可以使用该属性。

string query = “Select * From Win32_Process Where Name = “ + processName;
ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
ManagementObjectCollection processList = searcher.Get();

foreach (ManagementObject obj in processList)
{
     string cmdLine = obj.GetPropertyValue("CommandLine").ToString();

     if (cmdLine == "target command line options")
     {
          // do work
     }
}

The WMI Win32_ProcessObject has a CommandLine property you could use if that is what you know differentiates the instances.

string query = “Select * From Win32_Process Where Name = “ + processName;
ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
ManagementObjectCollection processList = searcher.Get();

foreach (ManagementObject obj in processList)
{
     string cmdLine = obj.GetPropertyValue("CommandLine").ToString();

     if (cmdLine == "target command line options")
     {
          // do work
     }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文