5

Is there any way to check from .NET if windows update is enabled?

I want to prompt the users every time they log into my app that their computer might be at risk and give them a link to windows update website (or windows update application from control panel).

Preferably it should work on XP, Vista and Windows 7. Maybe there is a registry key or even better an API?

Ovi
  • 2,620
  • 7
  • 34
  • 51

6 Answers6

8

First add a reference to WUApiLib "C:\windows\system32\Wuapi.dll"

Then you can use this code snippet.

WUApiLib.AutomaticUpdatesClass auc = new WUApiLib.AutomaticUpdatesClass();
bool active = auc.ServiceEnabled;

MSDN: "The ServiceEnabled property indicates whether all the components that Automatic Updates requires are available."

The Setting auc.Settings.NotificationLevel contains the information about the current mode. http://msdn.microsoft.com/en-us/library/aa385806(VS.85).aspx

Michael Piendl
  • 2,864
  • 1
  • 27
  • 21
  • I get this error on Windows 7 x64 Ultimate when trying your after referencing this Dlls. The type WUApiLib.AutomaticUpdatesClass has no constructors defined Kindly help. Thanks – Steve Johnson Oct 31 '12 at 20:19
6

Of course it is your choice to do that but getting prompted every few minutes that WindowsUpdate was switched off was by far the worst usability issue in XP.

You don't want to irritate your users. You should love them. And definitely not intrude in their private affairs like checking out if WU is off because honestly it's no business of yours.

User
  • 30,403
  • 22
  • 79
  • 107
3

I believe you can do this by using the Windows Update Agent API.

See IAutomaticUpdates for specifics.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
2

You can check the following registry key.

HKEY_LOCAL_MACHINE
  SOFTWARE
   Microsoft
     Active Setup
       Installed Components
         {89820200-ECBD-11cf-8B85-00AA005B4340}

If its IsInstalled value is 1 then Windows Update is installed.

This was taken from:

http://windowsitpro.com/article/articleid/15266/how-can-i-detect-if-windows-update-is-installed-on-a-machine.html

I actually REALLY like another answer to this question, but unfortunately, it's only supported on XP SP3, which might not be feasible.

Community
  • 1
  • 1
casperOne
  • 73,706
  • 19
  • 184
  • 253
1

Alternatively you can check to see if the Windows Update service is running using the PROCESS objects.

Something along these lines:

Function CheckIfServiceIsRunning(ByVal serviceName As String) As Boolean
   Dim mySC As ServiceProcess.ServiceController
   mySC = New ServiceProcess.ServiceController(serviceName)
   If mySC.Status = ServiceProcess.ServiceControllerStatus.Stopped Then
      ' Service isn't running
      Return False
   ElseIf mySC.Status = ServiceProcess.ServiceControllerStatus.Running Then
      ' Service already running
      Return True
   End If
End Function

if memory serves, the applicable service is named "Wuauserv"

Stephen Wrighton
  • 36,783
  • 6
  • 67
  • 86
0

Michael Piendl's answer didn't work for me, but it gave me the information I needed to make it work:

WUApiLib.AutomaticUpdatesClass auc = new WUApiLib.AutomaticUpdatesClass();<br>
string notificationLevel = auc.Settings.NotificationLevel.ToString();<br><br>

The notificationLevel string will equal different things depending on what option is chosen from the "Automatic Updates" dialog.

Rndm
  • 6,710
  • 7
  • 39
  • 58