AppleSeeker's Tech Blog
Welcome to AppleSeeker's space

在看该文章之前,如果你没有看过我之前的文章,请看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        forint 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。

posted on 2007-07-17 16:17  AppleSeeker(冯峰)  阅读(3973)  评论(30编辑  收藏  举报