TimeStamp_Sys100NS从Win32_PerfFormattedData_PerfProc_Process wmi返回null

时间:2012-12-05 17:10:57

标签: c# wmi wmi-query wmi-service

我正在尝试使用wmi查询返回TimeStamp属性并获取对象引用错误。当我查看quickwatch时,该属性本身为null。我已经在网上看到了这个例子,并且无法弄清楚为什么这个属性是teturning null,同时给了我查询的其他所有内容。

我使用的是Windows 7机器

   public List<string> ReturnProcesses()
    {
        string processname;

        ulong N1 = 0;
        ulong D1 = 0;
        ulong N2 = 0;
        ulong D2 = 0;

        ConnectionOptions options = new ConnectionOptions();

        options.Impersonation = ImpersonationLevel.Impersonate;
        List<string> Processlist = new List<string>();
        ManagementScope scope = new ManagementScope("\\root\\cimv2");
        scope.Connect();
        ObjectQuery query = new ObjectQuery("SELECT IDProcess,Name,PrivateBytes,PercentProcessorTime,TimeStamp_Sys100NS FROM Win32_PerfFormattedData_PerfProc_Process");
        ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
        ManagementObjectCollection queryCollection = searcher.Get();
        try
        {
            foreach (ManagementObject mo in queryCollection)
            {
                ulong privbytes = (ulong.Parse(mo["PrivateBytes"].ToString()) / 1024);
                processname = mo["IDProcess"].ToString() + " " + mo["Name"].ToString() + " " + mo["PercentProcessorTime"].ToString() + " " + privbytes.ToString();
                N2 = ulong.Parse(mo["PercentProcessorTime"].ToString());
                D2 = ulong.Parse(mo["TimeStamp_Sys100NS"].ToString());
                Processlist.Add(processname);
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
        return Processlist;
    }

1 个答案:

答案 0 :(得分:1)

您需要使用Win32__PerfRawData_PerfProc_Process代替Win32_PerfFormattedData_PerfProc_Process