gpt4 book ai didi

c# - WMI - 从 Win32_Product 中选择需要很长时间

转载 作者:行者123 更新时间:2023-11-30 13:30:55 25 4
gpt4 key购买 nike

我正在使用 WMI 枚举已安装的应用程序,无论我如何构建它,这个 block 都需要相对较长的时间才能完成。在我的环境中每次需要 13 秒。有没有更好(更快)的方法来检查程序是否已安装? (我使用 iTunes 作为示例程序来检查)

    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);
}

SELECT * FROM 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

SELECT * FROM Win32_Product WHERE name LIKE 'iTunes'

0 - 08:16:38.2719
1 - 08:16:38.2899
2 - 08:16:38.2999
3 - 08:16:51.5113
Length - 1

SELECT name FROM Win32_Product WHERE name LIKE 'iTunes'

0 - 08:19:53.9144
1 - 08:19:53.9324
2 - 08:19:53.9394
3 - 08:20:07.2794
Length - 1

最佳答案

如果您查询“Win32_product”msi 安装程序会检查并验证每个产品。

知识库文章 http://support.microsoft.com/kb/974524显示:

Win32_product Class is not query optimized. Queries such as “select * from Win32_Product where (name like 'Sniffer%')” require WMI to use the MSI provider to enumerate all of the installed products and then parse the full list sequentially to handle the “where” clause. This process also initiates a consistency check of packages installed, verifying and repairing the install. With an account with only user privileges, as the user account may not have access to quite a few locations, may cause delay in application launch and an event 11708 stating an installation failure.

Win32reg_AddRemovePrograms is a much lighter and effective way to do this, which avoids the calls to do a resiliency check, especially in a locked down environment. So when using Win32reg_AddRemovePrograms we will not be calling on msiprov.dll and will not be initiating a resiliency check.

所以要小心“Win32_product”。

更新:不错的文章https://sdmsoftware.com/group-policy-blog/wmi/why-win32_product-is-bad-news/

关于c# - WMI - 从 Win32_Product 中选择需要很长时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25083520/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com