Windows自動更新API (2)

來源:互聯網
上載者:User

在看該文章之前,如果你沒有看過我之前的文章,請看Windows自動更新API (1) 。

這篇文章主要內容介紹自動更新搜尋類:UpdateSearcherClass

UpdateSearcherClass:搜尋可以用的更新以及安裝過的更新

搜尋安裝過的更新:
用到QueryHistory(int startIndex, int Count)方法,startIndex -- 索引第幾個開始(從0開始),Count -- 尋找多少個(可以輸入超過安裝更新的數量,比如1000,都不會出錯,如果Count大於更新的總個數,則返回從該Index開始到最後的所有集合)。
返回結果為IUpdateHistoryEntryCollection集合類型,集合裡的每一個元素為IUpdateHistoryEntry2類型,存放每個更新的詳細資料。

搜尋當前可以安裝的更新:
用到Search(string criteria)方法,criteria該參數為需要尋找的過濾項。你可以參照IUpdate3類型理解,比如IUpdate3類型有IsInstalled屬性(bool類型),0表示false,可以將該參數設定為"IsInstalled = 0",表示只搜尋未安裝的所有更新程式(就像Sql語句的where項),如果滿足2項及以上須用到And。
比如:"Type = 1 And IsInstalled = 0",表示搜尋未安裝的軟體更新。

參考代碼:

 1public static void AutoUpdateSearch()
 2{
 3    try
 4    {
 5        WUApiLib.UpdateSearcherClass upSearch = new WUApiLib.UpdateSearcherClass();
 6
 7        if( upSearch.Online )
 8        {
 9            WUApiLib.IUpdateHistoryEntryCollection upHistoryEntColl = upSearch.QueryHistory(0,10);
10
11            foreach(WUApiLib.IUpdateHistoryEntry2 upHistoryEnt in upHistoryEntColl)
12            {
13                foreach(WUApiLib.ICategory category in upHistoryEnt.Categories)
14                {
15                    System.Console.WriteLine("   Category Name : {0}", category.Name);
16                    System.Console.WriteLine("   Category Parent : {0}", category.Parent);
17                    System.Console.WriteLine("   Category Description : {0}", category.Description);
18                    System.Console.WriteLine("   Category Type : {0}", category.Type);
19                }
20                System.Console.WriteLine("Update Title : {0}", upHistoryEnt.Title);
21                System.Console.WriteLine("Description : {0}", upHistoryEnt.Description);
22                System.Console.WriteLine("SupportUrl : {0}", upHistoryEnt.SupportUrl);
23                System.Console.WriteLine("Date : {0}", upHistoryEnt.Date.ToLongDateString());
24                System.Console.WriteLine("");
25            }
26        }
27
28        WUApiLib.ISearchResult resSearch = upSearch.Search("IsInstalled = 0");
29
30        for( int i = 0;i<resSearch.Updates.Count;i++)
31        {
32            System.Console.WriteLine("Update Title : {0}", resSearch.Updates[i].Title);
33            System.Console.WriteLine("Update Type : {0}", resSearch.Updates[i].Type.ToString());
34            System.Console.WriteLine("Max Download Size : {0}", resSearch.Updates[i].MaxDownloadSize);
35            System.Console.WriteLine("Min Download Size : {0}", resSearch.Updates[i].MinDownloadSize);
36            System.Console.WriteLine("Is Download : {0}", resSearch.Updates[i].IsDownloaded);
37        }
38    }
39    catch(Exception ex)
40    {
41        System.Console.WriteLine(ex.Message);
42    }
43}

糾正第一篇文章中UpdateSessionClass中關於代理的描述,Windows Update程式可以設定代理的使用者名稱和密碼,但前提是你將使用者名稱密碼輸入在IE提示對話方塊,Vista下,如果沒有輸入在IE提示對話方塊,在更新時也會讓你輸入。

文中代碼均在Vista下測試通過,用到DLL為Vista下的wuapi.dll。

如果大家在開發過程中不知道如何寫可以參照Windows自動更新API (1) 中的類圖表示,就能很好的理解該如果下手。

除擷取自動更新的配置資訊外,其他情況請都加上異常處理。
特別常見的情況:如果PC機的自動更新服務關閉,則搜尋時會報異常,無法調用COM。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.