前幾天寫過關於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瀏覽器中的代理設定串連,但是當Proxy 伺服器需要使用者名稱和密碼呢?
這樣只能通過程式來設定了。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)