2013-05-01 98 views
0

我目前想弄清楚,如何將Windows更新設置爲「讓我選擇是否安裝」,而不是在Windows「自動安裝更新」 8.如何以編程方式更改Windows更新選項?

根據Check from .NET if Windows Update is enabled我想:

WUApiLib.AutomaticUpdatesClass auc = new WUApiLib.AutomaticUpdatesClass(); 
// Doing some stuff 

但得到以下錯誤:
Interop type 'WUApiLib.AutomaticUpdatesClass' cannot be embedded. Use the applicable interface instead.

The type 'WUApiLib.AutomaticUpdatesClass' has no constructors defined

Change windows updates setting with Powershell答案我做:

string subKey = @"SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate\AU"; 
using (RegistryKey key = Registry.LocalMachine.OpenSubKey(subKey, true)) 
    key.SetValue("AUoptions", 4); 

但該子項不中導致Reference not set to an instance of an object註冊表錯誤存在。

Google的其餘結果都描述瞭如何手動更改此設置,這不是我正在尋找的。

如何以編程方式將Windows更新設置爲「讓我選擇是否安裝」?

+1

好,爲了擺脫互操作錯誤,右鍵單擊Visual Studio中的引用並轉到它的屬性,然後將「嵌入互操作類型」設置爲false。 – Arran 2013-05-01 11:48:41

+0

@Arran我看到了,現在第一個選項至少起作用。很高興知道,謝謝!現在我終於可以恢復工作了:D – Nolonar 2013-05-01 11:51:12

回答

3

感謝Arran,我設法讓在正確的方向邁出的一步:

好擺脫互操作錯誤,用鼠標右鍵單擊在Visual Studio中引用,去它的屬性,並反過來「嵌入互操作類型」爲false。

既然我不再發生互操作錯誤,我設法得出結論;下面的代碼:

// using WUApiLib; 
AutomaticUpdatesClass auc = new AutomaticUpdatesClass(); 
auc.Settings.NotificationLevel = AutomaticUpdatesNotificationLevel.aunlNotifyBeforeInstallation; 
auc.Settings.Save(); 
+0

+1,做得很到位:) – Arran 2013-05-01 17:55:04

+0

太好了。幫助我很多。對於投票的人來說,爲什麼你做到了?這對你來說可能完全沒有價值,但其他人(比如我)發現它非常有幫助。 – swdev 2014-04-11 00:19:26

0

可避免「互操作型......不能嵌入」錯誤留下「嵌入互操作類型」真的,從你的代碼省略後綴。而不是new AutomaticUpdatesClass()

使用new AutomaticUpdates()省去類後綴的更好的說明,請參見this answer。他們說.NET 4.0,但它也適用於4.5.1。

例如:

// using WUApiLib; 
AutomaticUpdates auc = new AutomaticUpdates(); 
auc.Settings.NotificationLevel = AutomaticUpdatesNotificationLevel.aunlNotifyBeforeInstallation; 
if (!auc.Settings.ReadOnly) 
    auc.Settings.Save(); 

我注意到,當我打開「嵌入互操作類型」爲假,也切換到「複製本地」設置爲true。使用類似於上面的代碼(和Embed = true),我能夠在Win7,8和10上查詢NotificationLevel,而無需在應用程序中部署任何版本的「wuapilib.dll」。

相關問題