在前一篇文中講述了搜尋已經安裝軟體和未安裝軟體。未安裝軟體是通過到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屬性:用來設定Proxy 位址
void PromptForCredentials(object parentWindow, string Title)方法:WinForm程式調用,指定父表單和彈出表單的Title,調用該方法後會彈出對話方塊,用來讓使用者輸入使用者名稱和密碼。