How to reliably check whether Windows Update is enabled using C#?

c#, com, windows-update

Solution

In your Visual Studio project References list, find the WUApiLib reference and change its "Embed Interop Types" to "False".

Problem

I tried to check if Windows Update is enabled. I added a reference to c:\windows\system32\wuapi.dll on Windows 7 x64 Ultimate and wrote this code ``` using WUApiLib; public Boolean IsWindowsUpdateEnabled() { var updates = new AutomaticUpdatesClass(); return updates.ServiceEnabled; } ``` The code fails to build. I get the following error: Error 1 The type 'WUApiLib.AutomaticUpdatesClass' has no constructors defined Error 2 Interop type 'WUApiLib.AutomaticUpdatesClass' cannot be embedded. Use the applicable interface instead. Error 3 'WUApiLib.AutomaticUpdatesClass' does not contain a definition for 'ServiceEnabled' and no extension method 'ServiceEnabled' accepting a first argument of type 'WUApiLib.AutomaticUpdatesClass' could be found (are you missing a using directive or an assembly reference?)

Original source