微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – 查询已安装的Windows更新的准确和本地化列表

如何使用C#查询安装在计算机上的 Windows更新的准确和本地化列表?

我将精确定义为匹配Windows 7中“程序和功能”下Microsoft的“View Installed Updates”对话框的“Microsoft Windows”类别中显示内容.

如果我使用WUApi.DLL,信息将被返回本地化但我无法获得准确的列表.在WUApi.dll的情况下,一些修补程序丢失,如果已卸载更新,它仍会显示在由以下代码生成的列表中:

public static void GetwindowsUpdates() 
{ 
    var updateSession = new UpdateSession(); 
    var updateSearcher = updateSession.CreateUpdateSearcher(); 
    var count = updateSearcher.GetTotalHistoryCount(); 
    if (count == 0) 
        return; 

    var history = updateSearcher.QueryHistory(0,count); 
    for (int i = 0; i < count; i++) 
    { 
        if (history[i].ResultCode == OperationResultCode.orcSucceeded) 
        { 
            Console.WriteLine(history[i].Title); 

            if (history[i].Operation == UpdateOperation.uoUninstallation) 
            { 
                Console.WriteLine("!!! Operation == uninstall"); // This is never true 
            } 
        } 
    } 
}

WUApi搜索方法也没有使用以下代码提供准确的列表:

WUApiLib.UpdateSessionClass session = new WUApiLib.UpdateSessionClass(); 
        WUApiLib.IUpdateSearcher searcher = session.CreateUpdateSearcher(); 

        searcher.IncludePotentiallySupersededUpdates = true; 

        WUApiLib.ISearchResult result = searcher.Search("IsInstalled=1"); 
        Console.WriteLine("Updates found: " + result.Updates.Count); 
        foreach (IUpdate item in result.Updates) 
        { 
            Console.WriteLine(item.Title); 
        }

如果我使用WMI来读取更新列表,我可以获得准确的列表,但它没有本地化.我使用以下代码

ManagementObjectSearcher searcher = new ManagementObjectSearcher(new ObjectQuery("select * from Win32_QuickFixEngineering")); 
searcher.Options.UseAmendedQualifiers = true; 
searcher.Scope.Options.Locale = "MS_" + CultureInfo.CurrentCulture.LCID.ToString("X"); 
ManagementObjectCollection results = searcher.Get(); 

Console.WriteLine("\n==WMI==" + results.Count); 
foreach (ManagementObject item in results) 
{ 
    Console.WriteLine("\t--Properties--"); 
    foreach (var x in item.Properties) 
    { 
        Console.WriteLine(x.Name + ": " + item[x.Name]); 
    } 
    Console.WriteLine("\t--System Properties--"); 
    foreach (var x in item.SystemProperties) 
    { 
        Console.WriteLine(x.Name + ": " + x.Value); 
    } 
    Console.WriteLine("\t--Qualifiers--"); 
    foreach (var x in item.Qualifiers) 
    { 
        Console.WriteLine(x.Name + ": " + x.Value); 
    } 
}

解决方法

WUApi只注册通过WUApi完成的操作,因此如果您手动安装或删除更新,它将在卸载后保留在列表中,或者永远不会显示在列表中.因此,在我看来,WUApi不能指望准确的清单.

WMI允许访问准确的Windows更新列表,但该列表仅过滤为“Microsoft Windows”类别.这很困难,因为我的要求是获取所有更新的列表.

在“View Installed Updates”对话框内部使用CBS(基于组件的服务).不幸的是,CBS不公开.有关API的一些详细信息,请访问:
http://msdn.microsoft.com/en-us/library/Aa903048.aspx

原文地址:https://www.jb51.cc/csharp/100301.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐