WMI-从Win32_Product中选择需要很长时间 [英] WMI - select from Win32_Product takes a long time
问题描述
我正在枚举使用WMI安装的应用程序,并且无论我如何构造它,此块都需要花费较长的时间才能完成.每次在我的环境中花费13秒.有没有更好(更快)的方法来检查程序是否已安装?(我使用iTunes作为示例程序进行检查)
I am enumerating installed applications using WMI, and this block is taking a relatively long time to complete no matter how I structure it. It takes 13 seconds in my environment every time. Is there a better (faster) way to check if a program is installed? (I'm using iTunes as an example program to check for)
private static string Timestamp
{
get { return DateTime.Now.ToString("HH:mm:ss.ffff"); }
}
private static void LoadInstalledPrograms()
{
List<string> installedPrograms = new List<string>();
Console.WriteLine("0 - {0}", Timestamp);
ManagementObjectSearcher mos = new ManagementObjectSearcher("SELECT * FROM Win32_Product");
Console.WriteLine("1 - {0}", Timestamp);
ManagementObjectCollection managementObjectCollection = mos.Get();
Console.WriteLine("2 - {0}", Timestamp);
foreach (ManagementObject mo in managementObjectCollection)
{
installedPrograms.Add(mo["Name"].ToString());
}
Console.WriteLine("3 - {0}", Timestamp);
Console.WriteLine("Length - {0}", installedPrograms.Count);
}
选择*来自Win32_Product
0 - 08:08:51.3762
1 - 08:08:51.3942
2 - 08:08:51.4012
3 - 08:09:04.8326
Length - 300
SELECT * FROM Win32_Product WHERE name ='iTunes'
0 - 08:14:17.6529
1 - 08:14:17.6709
2 - 08:14:17.6779
3 - 08:14:31.0332
Length - 1
选择*从Win32_Product,名称类似于"iTunes"
0 - 08:16:38.2719
1 - 08:16:38.2899
2 - 08:16:38.2999
3 - 08:16:51.5113
Length - 1
从Win32_Product中选择名称,其中的名称类似于"iTunes"
0 - 08:19:53.9144
1 - 08:19:53.9324
2 - 08:19:53.9394
3 - 08:20:07.2794
Length - 1
推荐答案
WMI正在花费时间,您已经注意到了.遍历注册表可能会为您解决问题.
WMI is taking it's time as you already noticed. Iterating through the registry might do the trick for you.
您可以在此处获取已安装的应用程序在stackoverflow上,其中提到了两种方法.
You might have a look at Get installed applications in a system here on stackoverflow, where both methods are mentioned.
这篇关于WMI-从Win32_Product中选择需要很长时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!