Windows自動更新API (3)

來源:互聯網
上載者:User

在前一篇文中講述了搜尋已經安裝軟體和未安裝軟體。未安裝軟體是通過到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,調用該方法後會彈出對話方塊,用來讓使用者輸入使用者名稱和密碼。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.