AppleSeeker's Tech Blog
Welcome to AppleSeeker's space

在前一篇文中讲述了搜索已经安装软件和未安装软件。未安装软件是通过到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方法就开始搜索了。

参考代码:

 1public 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,调用该方法后会弹出对话框,用来让用户输入用户名和密码。

posted on 2007-07-19 22:08  AppleSeeker(冯峰)  阅读(3968)  评论(0编辑  收藏  举报