【问题标题】:How to access system process properties in C#?如何在 C# 中访问系统进程属性?
【发布时间】:2015-10-13 13:21:36
【问题描述】:

我想找到系统进程的可执行路径,我遵循 WMI 方法,我使用 Process.EnterDebugMode() 让我的进程获得访问操作系统进程属性的权限,但是当我调用 ManagementObject.GetPropertyValue("ExecutablePath ") 返回 null ,我正在为 smss.exe 进程尝试这个。

代码如下:

    static void Main(string[] args)
    {

        int processID = 536; // Just trying with the PID of smss.exe running in my system

        System.Diagnostics.Process.EnterDebugMode();

        ConnectionOptions options = new ConnectionOptions();
        options.EnablePrivileges = true;
        options.Impersonation = ImpersonationLevel.Impersonate;

        ManagementScope scope = new ManagementScope(@"\\.\root\cimv2", options);

        string NameProperty = "Name";
        string PidProperty = "ProcessId";
        string FullPathProperty = "ExecutablePath";
        string query;
        query = string.Format("select {0}, {1}, {2} from Win32_Process where {1} = {3}", NameProperty, PidProperty, FullPathProperty, processID);
        ObjectQuery objectQuery = new ObjectQuery(query);
        ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, objectQuery);

        foreach (ManagementObject processEntry in searcher.Get())
        {
            if (processEntry.GetPropertyValue(PidProperty) != null)
            {
                int value = Convert.ToInt32(processEntry.GetPropertyValue(PidProperty));
                if (value == processID)
                {
                    if (processEntry.GetPropertyValue(FullPathProperty) != null)
                    {
                        System.Windows.Forms.MessageBox.Show(processEntry[FullPathProperty].ToString());
                    }

                }
            }
        }    
    }

【问题讨论】:

    标签: windows wmi


    【解决方案1】:

    您无法使用wmi 获取smss.exe 进程的此信息:

    wmic process where "Caption='smss.exe'" get /value
    

    上面的命令以及任务管理器显示空的CommandLineExecutablePath

    ==> wmic process where "Caption='smss.exe'" get Caption, CommandLine, ExecutablePath, ProcessID
    Caption   CommandLine  ExecutablePath  ProcessId
    smss.exe                               248
    

    但是,您可以使用where command 获得smss.exe 的可执行路径(无用):

    ==> where smss.exe
    C:\Windows\System32\smss.exe
    

    Critical System Services MSDN 文章中的更多信息。

    关键系统服务无法由 重新启动管理器,无需重新启动系统。更新任何文件或 这些服务之一正在使用的资源需要重新启动系统。

    【讨论】:

    • 除了WMI还有其他方法吗,性能计数器功能可以帮助吗?
    猜你喜欢
    • 1970-01-01
    • 2014-03-10
    • 2011-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-24
    • 1970-01-01
    • 2016-07-22
    相关资源
    最近更新 更多