Question

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"?

Was it helpful?

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();

OTHER TIPS

You can avoid the "Interop type ... cannot be embedded" error by leaving "Embed Interop Types" true, and omitting the Class suffix from your code.

Use new AutomaticUpdates() instead of new AutomaticUpdatesClass()

See this answer for a better description of omitting the class suffix. They say .Net 4.0, but it also works for me in 4.5.1.

ex:

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

I noticed that when I switched "Embed Interop Types" to false, it also switched "Copy Local" to true. With code similar to the above (and Embed = true), I was able to query the NotificationLevel on Win7, 8, and 10 without deploying any version of "wuapilib.dll" alongside my application.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top