利用WMI,如何高效获得系统进程的用户名?高分求教!
我用这种方法得到相应进程的相关信息,其中用InvokeMethod()的方法得到进程用户名,但是效率相对太低(要多执行近1秒),希望高手提供一个效率高的方法得到这个项,最好用WQL直接查询。
ManagementObjectCollection processBase = WmiQuery("*", "Win32_Process");
foreach(ManagementObject obj in processBase)
{
Console.Write(obj.Properties["ProcessID"].Value.ToString());
Console.Write(obj.Properties["Name"].Value.ToString());
if (obj.Properties["ProcessID"].Value.ToString() != "0")
{
object user = obj.InvokeMethod("GetOwner", null, new InvokeMethodOptions(null, TimeSpan.MaxValue));
Console.Write(((ManagementBaseObject)user).Properties["User"].Value.ToString());
}
else
Console.Write("SYSTEM");
Console.Write(bufferTable[obj.Properties["ProcessID"].Value.ToString()].ToString());
Console.WriteLine(obj.Properties["WorkingSetSize"].Value.ToString());
}
其中:protected ManagementObjectCollection WmiQuery(string sltItems, string sltTarget)
{
ManagementObjectSearcher mSearCher = new ManagementObjectSearcher();
mSearCher.Scope = new ManagementScope(@"\\localhost\root\cimv2");
mSearCher.Query = new ObjectQuery(string.Format("select {0} from {1}", sltItems, sltTarget));
return mSearCher.Get();
}