尊敬的Stackoverflow社区,

我是编程的初学者,正在构建一个小型应用程序。

我遇到以下问题:


输入:正在运行的进程的ID
输出:运行该进程时已执行命令(如图所示)。


在我的应用程序中,我尝试使用:System.Diagnostics.Process和System.Management.ManagementObject,但是找不到要查找的属性,

有人建议我解决这个问题,我将不胜感激。

谢谢。

See pictures

最佳答案

您使用System.Management.ManagementObject处在正确的轨道上,并且正在寻找CommandLine属性。您需要将构造函数的WMI路径传递给对象,在本例中为Win32_Process.Handle=6316。例如:

string GetProcessCommandLine(int processId) =>
    System.Management.ManagementObject("Win32_Process.Handle=$processId").CommandLine;




或者,根据https://serverfault.com/questions/696460/given-a-pid-on-windows-how-do-i-find-the-command-line-instruction-that-execute,您可以执行以下WMI查询:

SELECT CommandLine FROM Win32_Process WHERE ProcessID = <your process ID>


您可以使用System.Management.ManagementObjectSearcher从C#完成此操作。最后,无论如何,这将返回与上述相同的ManagementObject(仅填充CommandLine属性)。例如,应执行以下操作:

string GetCommandLine(int processId) =>
    System.Management.ManagementObjectSearcher(
        "select CommandLine from Win32_Process where ProcessID = $processId")
    .Get()[0]
    .CommandLine;

关于c# - C#如何检索通过进程ID运行的进程的“命令行”(执行语句),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49788768/

10-10 23:30
查看更多