Exemple #1
0
    public static ProcessInfo GetProcessInfo(ManagementScope scope, string processName)
    {
        ProcessInfo processInfo = new ProcessInfo();

        processInfo.Name = processName;
        ObjectQuery query1 = new ObjectQuery("Select Name,PercentProcessorTime,Timestamp_Sys100NS From Win32_PerfRawData_PerfProc_Process Where Name='" + processName + "'");
        ManagementObjectSearcher managementObjectSearcher = new ManagementObjectSearcher(scope, query1);
        ulong num1 = 0;
        ulong num2 = 0;
        ulong num3 = 0;

        using (ManagementObjectCollection.ManagementObjectEnumerator enumerator = managementObjectSearcher.Get().GetEnumerator())
        {
            if (enumerator.MoveNext())
            {
                ManagementObject current = (ManagementObject)enumerator.Current;
                num1 = (ulong)current["PercentProcessorTime"];
                if (current["Timestamp_Sys100NS"] != null)
                {
                    num2 = (ulong)current["Timestamp_Sys100NS"];
                }
            }
        }
        Thread.Sleep(1000);
        using (ManagementObjectCollection.ManagementObjectEnumerator enumerator = new ManagementObjectSearcher(scope, query1).Get().GetEnumerator())
        {
            if (enumerator.MoveNext())
            {
                ManagementObject current = (ManagementObject)enumerator.Current;
                ulong            num4    = (ulong)current["PercentProcessorTime"];
                if (current["Timestamp_Sys100NS"] != null)
                {
                    num3 = (ulong)current["Timestamp_Sys100NS"];
                }
                processInfo.PercentCPU = new ulong?((num4 - num1) * 100UL / (num3 - num2));
            }
        }
        ObjectQuery query2 = new ObjectQuery("Select Name, CreationDate From WIN32_Process");

        foreach (ManagementObject managementObject in new ManagementObjectSearcher(scope, query2).Get())
        {
            if (((string)managementObject["Name"]).Split('.')[0] == processName)
            {
                string dmtfDate = managementObject["CreationDate"].ToString();
                processInfo.CreationDate = WMIUtils.ToDateTime(dmtfDate);
                break;
            }
        }
        return(processInfo);
    }