c# - 如何以编程方式更改 Windows 更新选项?

标签 c# windows windows-8 windows-update

我目前正在尝试弄清楚如何在 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 更新设置为“让我选择是否安装”?

最佳答案

感谢Arran ,我设法朝着正确的方向迈出了一步:

Well to get rid of the interop error, right-click the reference in Visual Studio and go it's properties, and turn "Embed Interop Types" to false.

现在我不再遇到互操作错误,我设法得出结论;这是代码:

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

关于c# - 如何以编程方式更改 Windows 更新选项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16317510/

相关文章:

c# - ActionFilterAttribute 添加的 Headers 不会出现在 ApiController 中

c# - 无法加载文件或程序集 'Microsoft.AI.Agent.Intercept' 或其依赖项之一

c# - 如何在2个Windows服务之间形成依赖关系

java - 如何根据使用的字体将代码点映射到 unicode 字符?

c# - 在 C# 中如何旋转图片框及其图像?

c# - 如何获取 Windows 服务登录用户的安全 token ?

c - 使用 Visual Studio 2010,如何向 Visual C++ Win32 控制台应用程序提供输入?

c# - 我们如何检测 WIndows 8 中的系统音量?

wpf - MVVM 模式 - 执行 View 操作

c# - 如何使用用户控件后台代码中存在的事件处理程序以编程方式打开和关闭 bottomappbar?