Visual Studio 2008 , Vista で、GetProcesses() でプロセス情報を取得する際に、TotalProcessorTime などプロセッサタイムを取得しようとすると、Win32Exception により取得できない。なぜ??
var query = from proc in System.Diagnostics.Process.GetProcesses()
orderby proc.Id
select new
{
Id = proc.Id,
processName = proc.ProcessName,
TotalProcessorTime = proc.TotalProcessorTime
};
foreach (var process in query)
{
Console.WriteLine(process.Id + ":");
Console.WriteLine(process.processName + ":");
Console.WriteLine(process.TotalProcessorTime + ":");
}
—————–
WMI を使用すれば、取得可能。
ManagementObjectSearcher query1 = new ManagementObjectSearcher("SELECT * FROM Win32_Process");
ManagementObjectCollection queryCollection1 = query1.Get();
foreach (ManagementObject mo in queryCollection1)
{
Console.WriteLine(mo["ProcessId"] + "\t" + mo["Name"] + "\t" +
":\tKernelModeTime=" + mo["KernelModeTime"] + // in 100 nanosecond units
":\tUserModeTime=" + mo["UserModeTime"]); // in 100 nanosecond units
}