2013-05-01 94 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”。

相关问题