2 回答

TA貢獻1844條經驗 獲得超8個贊
我建議遷移到較新的命名空間,因為System.Management
它較舊、速度較慢且無法擴展。您所追求的新框架是Microsoft.Management.Infrastructure
.?以下是Microsoft 文檔對此進行了解釋以及兩者的示例。
所以你會使用這樣的東西:
Using Microsoft.Management.Infrastructure;
CimSession Session = CimSession.Create("computer_name");
CimInstance Instance = Session.QueryInstances(@"root\cimv2", "WQL", "SELECT Name FROM Win32_ComputerSystem");
foreach (CimInstance i in Instance){
? ? Console.WriteLine(i.CimInstanceProperties["Name"].Value);
}
或者
Using Microsoft.Management.Infrastructure;
CimSession Session = CimSession.Create("computer_name");
CimInstance Instance = Session.QueryInstances(@"root\cimv2", "WQL", "SELECT Name FROM Win32_ComputerSystem").First();
Console.WriteLine(Instance.CimInstanceProperties["Name"].Value);
我希望這能給你一些新的兔子洞來解決:-D 如果你還需要什么,請告訴我們:)

TA貢獻2003條經驗 獲得超2個贊
我創建了獲取流程所有者的最終方法。它仍然不是超級快,但已經足夠快了。與我的問題中的上述 WMI 方法相比,速度提升了 60% 以上,我相信仍有改進的空間。
示例:從另一個 VLAN 中的工作站 獲取數據(進程所有者、ID、句柄、可執行路徑、描述、命令行),但同一網絡域且大約具有 1 個 VLAN。200個進程:
使用上面舊的 WMI 方法:大約。7000毫秒
使用以下新方法:大約。2400毫秒
方法:
public struct WMIProcessProperties
{
? ? public string Owner;
? ? public int ID;
}
public static async Task<Dictionary<Process, WMIProcessProperties>> GetWMIProperties(this IEnumerable<Process> processes)
{
? ? Dictionary<Process, WMIProcessProperties> result = new Dictionary<Process, WMIProcessProperties>();
? ? if (processes == null || processes.Count() == 0) { return result; }
? ? string selectQuery = "SELECT Handle, ProcessID FROM Win32_Process";
? ? selectQuery += processes.Count() <= 10 ? string.Format(" WHERE ProcessID = {0}", string.Join(" OR ProcessID = ", processes.Select(p => p.Id))) : string.Empty;
? ? using (CimSession session = await Task.Run(() => CimSession.Create(processes.ElementAt(0).MachineName)))
? ? {
? ? ? ? List<CimInstance> instances = await Task.Run(() => session.QueryInstances(@"root\cimv2", "WQL", selectQuery).ToList());
? ? ? ? List<Task<WMIProcessProperties>> tasks = new List<Task<WMIProcessProperties>>();
? ? ? ? for (int i = 0; i < instances.Count; i++)
? ? ? ? {
? ? ? ? ? ? CimInstance currentInstance = instances[i];
? ? ? ? ? ? tasks.Add(Task.Run(() =>
? ? ? ? ? ? {
? ? ? ? ? ? ? ? int id = Convert.ToInt32(currentInstance.CimInstanceProperties["ProcessID"].Value);
? ? ? ? ? ? ? ? string owner;
? ? ? ? ? ? ? ? using (CimMethodResult getOwnerResult = session.InvokeMethod(currentInstance, "GetOwner", null))
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ?owner = getOwnerResult.OutParameters["User"]?.Value?.ToString();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? currentInstance.Dispose();
? ? ? ? ? ? ? ? return new WMIProcessProperties { Owner = owner, ID = id };
? ? ? ? ? ? }));
? ? ? ? }
? ? ? ? WMIProcessProperties[] wmiProcessProperties = await Task.WhenAll(tasks).ConfigureAwait(false);
? ? ? ? for (int i = 0; i < wmiProcessProperties.Length; i++)
? ? ? ? {
? ? ? ? ? ? result.Add(processes.Single(p => p.Id == wmiProcessProperties[i].ID), wmiProcessProperties[i]);
? ? ? ? }
? ? }
? ? return result;
}
- 2 回答
- 0 關注
- 153 瀏覽
添加回答
舉報