Windows自动更新API (3)

在前一篇文中讲述了搜索已经安装软件和未安装软件。未安装软件是通过到MS的Update网站去得到信息的,但是如果客户端离线的情况想知道自己机器还缺哪些软件,该如何实现呢?下面就讲述离线搜索未安装软件的方法。

事先准备一个CAB文件,该文件是MS提供的所有的补丁包集合,针对所有操作系统。最好定期更新该CAB文件,因为MS随时会有补丁发布。
下载地址:wsusscan.cab

用到的类:UpdateSessionClass,UpdateServiceManagerClass,IUpdateService2,IUpdateSearcher2,ISearchResult,IUpdate3

创建一个UpdateSessionClass类,通过该类实例来创建搜索类。
创 建一个UpdateServiceManagerClass类,通过调用该类的AddScanPackageService方法,注册一个 Service,并指定扫描的CAB包,并返回IUpdateService2实例。该方法最后一个参数是指UpdateOperation枚举类型 (Install/UnInstall)
通过UpdateSessionClass的实例调用CreateUpdateSearcher方法创建IUpdateSearcher2实例。

关键:
指定IUpdateSearcher2实例的属性ServerSelection,这个属性是指搜索时,是搜索所有的,还是WindowsUpdate还是其它的,default包含所有的。
指定IUpdateSearcher2实例的ServiceId等于IUpdateService2实例的ServiceId。
调用Search方法就开始搜索了。

参考代码:

 1 public List<UnUpdatedEntry> ScanOfflineUpdates()
 2 {
 3    List<UnUpdatedEntry> list = new List<UnUpdatedEntry>();
 4
 5    WUApiLib.UpdateSessionClass upSession = new WUApiLib.UpdateSessionClass();
 6    WUApiLib.UpdateServiceManagerClass upServiceManager = new WUApiLib.UpdateServiceManagerClass();
 7    WUApiLib.IUpdateService2 upService = (WUApiLib.IUpdateService2)upServiceManager.AddScanPackageService("Offline Sync Service", @"d:/wsusscan.cab", 0);
 8    
 9    WUApiLib.IUpdateSearcher2 upSearch = (WUApiLib.IUpdateSearcher2)upSession.CreateUpdateSearcher();
10
11    upSearch.ServerSelection = WUApiLib.ServerSelection.ssDefault;
12
13    upSearch.ServiceID = upService.ServiceID;
14
15    WUApiLib.ISearchResult resSearch = upSearch.Search("IsInstalled=0");
16
17    foreach(WUApiLib.IUpdate3 update in resSearch.Updates)
18    {
19        UnUpdatedEntry entry = new UnUpdatedEntry();
20        entry.Title = update.Title;
21        entry.Type = update.Type.ToString();
22        entry.MaxSize = update.MaxDownloadSize;
23        entry.MinSize = update.MinDownloadSize;
24        entry.Download = update.IsDownloaded;
25
26        list.Add(entry);
27    }

28
29    return list;
30}

这里我将取出来的信息放入一个List<T>集合内,绑定到数据源控件中。

在下一篇中,我将讲述如何选择一个或多个补丁进行下载安装。上述代码在XP和Vista下测试通过,如果你的机器有代码,请通过调用UpdateSessionClass的WebProxy属性中
Address属性:用来设置代理地址
void PromptForCredentials(object parentWindow, string Title)方法:WinForm程序调用,指定父窗体和弹出窗体的Title,调用该方法后会弹出对话框,用来让用户输入用户名和密码。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值