0
在我的WPF應用程序中,我需要獲取具有特定屬性的Windows服務(〜200)列表。Wpf WMI Win32_Service查詢
var result = new List<ServicesModel>();
ConnectionOptions connOptions = new ConnectionOptions
{
Impersonation = ImpersonationLevel.Impersonate,
EnablePrivileges = true,
};
ManagementScope manScope = new ManagementScope([email protected]"\\{System.Environment.MachineName}\ROOT\CIMV2",
connOptions);
manScope.Connect();
SelectQuery query = new SelectQuery("SELECT * FROM Win32_Service");
using (var searcher = new ManagementObjectSearcher(manScope, query))
foreach (var o in searcher.Get())
{
var obj = (ManagementObject) o;
ServicesModel service = new ServicesModel
{
Name = obj["DisplayName"] as string,
Path = obj["PathName"] as string,
Description = obj["Description"] as string,
Pid = Convert.ToInt32(obj["ProcessId"]),
Status = obj["State"] as string,
StartMode = obj["StartMode"] as string,
LogOnAs = obj["StartName"] as string
};
result.Add(service);
}
return result;
執行此方法大約需要1分鐘左右,並返回不可接受的數據。 看起來像searcher.Get()需要所有的時間... 我可以做些什麼來提高執行/返回時間/性能?
感謝
我爲管理員重新啓動該盒子感到抱歉。 –
搜尋者很慢。你看過這篇文章嗎? http://stackoverflow.com/questions/10502879/wmi-call-takes-too-much-time-when-system-starts-restarts – Tim
謝謝蒂姆,你的評論幫了我很多! –