How to determine whether windows update is enabled or not with C# in WinForms

If you are looking for a way to check if Windows Update is enabled or disabled, you can use the WUAPILib of Windows. The Windows Update Agent (WUA) API is a set of COM interfaces that enable system administrators and programmers to access Windows Update and Windows Server Update Services (WSUS). Scripts and programs can be written to examine which updates are currently available for a computer, and then you can install or uninstall updates. WUA is supported starting with Windows XP. WUA is supported on the server starting with Windows Server 2003.

In this article, we'll explain you how to verify whether Windows Update is enabled or not using the WUApiLib dll.

1. Add reference to wuapi.dll

In order to use the API of the mentioned dll file, you will need to add it as reference on your project.  To do that, do Right Click on your Project in the solution explorer and click on Add References:

References .net C#

In the dialog that appears, click on Browse in the bottom:

This will open the filebrowser of the system, search for the wuapi.dll file in the C:\Windows\System32 directory and select it:

    Windows wuapi.dll file

    Confirm in the reference manager dialog:

    Confirm WuAPILib dll reference in C#

    And you will have now the reference to the file. However, you will see an exception in the code if your project tries to embed the interop type, because since .NET 4.0 allows primary interop assemblies (or rather, the bits of it that you need) to be embedded into your assembly so that you don't need to deploy them alongside your application. This means, the dll will be available on every system where your application can be deployed as the dll file makes part of Windows, so to fix this you will need to prevent the dll from being embed in your app. To do this, just open the References of the project in the solution explorer, search for WUApiLib and in the properties area set the Embed Interop Types property to False:

    C# Disabled Embed Interop Types

    If you don't do this, you will see the exception "Interop type 'WUApiLib' cannot be embedded. Use the applicable interface instead when you try to write some code using this API. Once this DLL has been referenced without embedding it, you are ready to use it !

    2. Check if Windows Update is enabled/disabled

    Now that you have access to the wuapi.dll in your project, just include it in your code with using WUApiLib and verify the status of Windows Update with the following logic:

    // Access the wuapi.dll with the namespace in C#
    using WUApiLib;
    
    AutomaticUpdatesClass AUC = new AutomaticUpdatesClass();
    bool isWUEnabled = AUC.ServiceEnabled;
    
    if (isWUEnabled)
    {
        Console.WriteLine("Windows Update is Enabled");
    }
    else
    {
        Console.WriteLine("Windows Update is Disabled");
    }

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

    Happy coding !

    This could interest you

    Become a more social person