Search code examples
c#windowswindows-8windows-update

How to programmatically change Windows update option?


I am currently trying to figure out, how to set Windows update to "let me choose whether to install" instead of "Install updates automatically" on Windows 8.

According to Check from .NET if Windows Update is enabled I tried:

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

But get following errors:
Interop type 'WUApiLib.AutomaticUpdatesClass' cannot be embedded. Use the applicable interface instead.

The type 'WUApiLib.AutomaticUpdatesClass' has no constructors defined

Following the answer in Change windows updates setting with Powershell I did:

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

But the subkey does not exist in the registry which leads to Reference not set to an instance of an object errors.

The rest of Google's results all describe how to manually change this setting, which is not what I'm looking for.

How can I programmatically set Windows Updates to "let me choose whether to install"?


Solution

  • Thanks to Arran, I managed to make a step in the right direction:

    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.

    Now that I no longer get interop errors, I managed to reach a conclusion; here's the code:

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