C# WMI 进程差异化?
场景
我有一个使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用将进程 ID 作为输出参数的技术来创建进程。例如
[out] uint32 ProcessId
然后您可以使用该值来真正了解稍后要杀死哪个版本的进程。例如
(请注意,如果该过程在您之前停止杀死它,操作系统可以将相同的进程ID分配给一个新进程,所以当然你需要仔细检查你是否杀死了正确的进程,例如也检查进程名称)
Create the process using a technique that gives you the process ID as an out parameter. E.g.
[out] uint32 ProcessId
Then you can use that value to truly know which version of the process you want to kill later. E.g.
(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)
WMI Win32_ProcessObject 有一个 CommandLine 属性,如果您知道该属性可以区分实例,则可以使用该属性。
The WMI Win32_ProcessObject has a CommandLine property you could use if that is what you know differentiates the instances.