Search code examples
c#automationwindows-update

How can you programatically disable Windows Update in XP using C#


I am working on a project and need to automate disabling Windows Automatic Update using my C# code. I am not quite sure where to start. I have read about possibly using the Windows Update Agent API but am not sure. Any advice would be greatly appreciated. Thanks in advance.


Solution

  • There is a windows service that is responsible for performing the updates, you can stop this service with the following code:

    ServiceController sc = new ServiceController("wuauserv");
    try
    {
        if (sc != null && sc.Status == ServiceControllerStatus.Running)
        {
            sc.Stop();
        }
        sc.WaitForStatus(ServiceControllerStatus.Stopped);
        sc.Close();
    }
    catch (Exception ex)
    {                
        Console.WriteLine(ex.Message);
    }
    

    You will need:

    1. Add a reference to the proyect selecting "Add Reference" into Project Name then Add System.ServiceProcess
    2. Include using System.ServiceProcess; in your class
    3. Run your application or Visual Studio as Administrator