AppleSeeker's Tech Blog
Welcome to AppleSeeker's space

前几天写过关于Vista下防火墙的设置,今天觉得防火墙都有对应的API,那自动更新是否有相应的API呢?Vista下的与Xp下的有多少不同呢?带着这个疑问开始一天的调查。

查了很多资料找到要操作Windows Update必须经由Windows Update Agent(WUA)。
在Vista和Xp下,该wuapi.dll都能找到(Windows\System32下),奇怪的是,Vista下该dll的版本是6.0,而Xp下是5.8,不知道改动点在哪里,粗略查看了里面的主要的类,发现并没改动什么,难道只是内部函数的调整?

要操作Windows Update相应类,必须了解他们之间的关系,如图所示:

从上图不难发现,AutomaticUpdatesClass只是自动更新的设置类,关于所有自动更新的类都通过UpdateSessionClass。

取得自动更新的所有设置,以及如何改变设置:
通过实例化一个AutomaticUpdatesClass类,然后访问它的Settings属性,该属性中:
NotificationLevel:自动更新的动作(自动/无效/通知后下载/下载后通知),枚举类型
ScheduledInstallationDay:自动下载是每星期几(从星期一到星期天),枚举类型
ScheduledInstallationTime:自动下载的时间(从0点到23点)

可以直接修改
AutomaticUpdatesNotificationLevel属性以及ScheduledInstallationDay和ScheduledInstallationTime
设置完后调用IAutomaticUpdatesSettings的Save方法。
IAutomaticUpdatesSettings实例通过AutomaticUpdatesClass对象的Settings属性获得。

在该AutomaticUpdatesClass类中,可以调用EnableService方法启动自动更新服务,只有当自动更新服务启动后,通过读取自动更新设置,来从MS网站获取更新内容。通常情况下,自动更新服务都是设置为自动启动。

当然你也可以通过程序启动,该服务启动后,自动设为自动启动(无论你原先是手动还是无效),MS没有提供方法将该服务停止关闭。

当服务启动后,可以使用下面3个方法:
DetectNow:开始更新
Pause:暂停更新
Resume:恢复更新

参考代码:

 1        public static void AutoUpdateSetting()
 2        {
 3            System.Console.WriteLine("AutoUpdate Imformation:");
 4
 5            WUApiLib.AutomaticUpdatesClass updateCls = new WUApiLib.AutomaticUpdatesClass();
 6
 7            //Show Update Dialog:
 8            updateCls.ShowSettingsDialog();
 9
10            WUApiLib.IAutomaticUpdatesSettings upSettings = updateCls.Settings;
11
12            //Show Update Setting -- Notification Level
13            // aunlNotConfigured = 0
14            // aunlDisabled = 1
15            // aunlNotifyBeforeDownload = 2
16            // aunlNotifyBeforeInstallation = 3
17            // aunlScheduledInstallation = 4
18            System.Console.WriteLine("Notification Level : {0}", upSettings.NotificationLevel.ToString());
19
20            if( upSettings.NotificationLevel == WUApiLib.AutomaticUpdatesNotificationLevel.aunlScheduledInstallation )
21            {
22                // ScheduledInstallationDay:
23                // ausidEveryDay = 0,
24                // ausidEverySunday = 1,
25                // ausidEveryMonday = 2,
26                // ausidEveryTuesday = 3,
27                // ausidEveryWednesday = 4,
28                // ausidEveryThursday = 5,
29                // ausidEveryFriday = 6,
30                // ausidEverySaturday = 7,
31
32                // ScheduledInstallationTime: 0 - 23
33                System.Console.WriteLine("Update Schedule Time : {0} , {1}"
34                        upSettings.ScheduledInstallationDay.ToString(), 
35                        upSettings.ScheduledInstallationTime.ToString());
36            }

37
38            upSettings.NotificationLevel = WUApiLib.AutomaticUpdatesNotificationLevel.aunlNotifyBeforeDownload;
39            upSettings.Save();
40
41            if( updateCls.ServiceEnabled )
42            {
43                System.Console.WriteLine("Update Service state : {0}", updateCls.ServiceEnabled.ToString());
44                //updateCls.EnableService();
45                System.Console.WriteLine("DetectNow");
46                updateCls.DetectNow();
47                
48                Thread.Sleep(10000);
49                System.Console.WriteLine("Pause");
50                updateCls.Pause();
51                
52                Thread.Sleep(10000);
53                System.Console.WriteLine("Resume");
54                updateCls.Resume();
55            }

56
57            System.Console.WriteLine();
58        }

大家都知道控制面板中的自动更新的设置是没有代理选项的,那到底自动更新支持代理吗?
当然支持,自动更新会根据IE浏览器中的代理设置连接,但是当代理服务器需要用户名和密码呢?
这样只能通过程序来设置了。UpdateSessionClass类就提供该功能。

实例化该类后,通过访问该类的WebProxy属性得到WebProxyClass类的实例。
可以设置WebProxyClass类的二个属性,并调用一个方法即可。
Address:代理的地址
UserName:用户名
SetPassword(string strPassword):设置密码

参考代码:
 1public static void AutoUpdateSession()
 2{
 3    WUApiLib.UpdateSessionClass upSessCls = new WUApiLib.UpdateSessionClass();
 4    
 5    System.Console.WriteLine("Update Session : {0}", upSessCls.ClientApplicationID);
 6    
 7    WUApiLib.WebProxyClass webProxy = (WUApiLib.WebProxyClass)upSessCls.WebProxy;
 8
 9    webProxy.Address = @"*********";
10    webProxy.UserName = "*******";
11    webProxy.SetPassword("******");
12
13    System.Console.WriteLine("WebProxy Address : {0}", webProxy.Address);
14    System.Console.WriteLine("WebProxy AutoDetect : {0}", webProxy.AutoDetect);
15    System.Console.WriteLine("WebProxy UserName : {0}", webProxy.UserName);
16}

当我调查到这里的时候,想做一个软件,可以自己来实现更多的操作,对自动更新来说。
大家有什么好的想法,可以告诉我,谢谢~~~。

文中有错误的地方,希望指正。

国内没有好的这方面的资料,可以参考MS的MSDN。
参考:
Interfaces (Windows)
Using the Windows Update Agent API (Windows)
Windows Update Agent Object Model (Windows)
posted on 2007-07-13 16:43  AppleSeeker(冯峰)  阅读(9724)  评论(32编辑  收藏  举报