c# 获取指定进程的命令行

using System;
using System.Management;

class Example {
    static void Main(string[] args) {
        string processName = "javaw";  // 进程名称,可根据实际情况进行修改

        ManagementObjectSearcher searcher =
                new ManagementObjectSearcher("SELECT CommandLine FROM Win32_Process WHERE Name='" + processName + ".exe'");
        foreach (ManagementObject process in searcher.Get()) {
            string commandLine = process["CommandLine"].ToString();
            Console.WriteLine("Process {0} started with command line: {1}", processName, commandLine);
        }
    }
}

 注意:要以管理员身份运行上述代码,否则部分进程获取到的结果为空

posted on 2023-05-17 09:25  空明流光  阅读(379)  评论(0编辑  收藏  举报

导航