views:

325

answers:

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?

+3  A: 

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

See IAutomaticUpdates for specifics.

Reed Copsey
+2  A: 

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.

casperOne
it could be installed and in mode "disabled"...
michl86
+1  A: 

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
+3  A: 

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
Who knew someone with a name like SO sucks would provide such sound advice? +1
BFree
Thanks. I've just did some rebranding.
User
+5  A: 

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

michl86