Click here to Skip to main content
15,898,374 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to know if WIndows Update is enabled C#?

I just need a Code in C# that show me if Windows Update is enabled or not. I have tried with this library WUApiLib but I'm not sure how to use it to know exactly I want.

Something like this:

C#
if(xxxxxxxxxxx)
{
MessageBox.Show("Windows Update is working");
}
else
{
MessageBox.Show("Windows Update is not working");
}


Thanks.

What I have tried:

C#
using WUApiLib;
protected override void OnLoad(EventArgs e){
    base.OnLoad(e);
    UpdateSession uSession = new UpdateSession();
    IUpdateSearcher uSearcher = uSession.CreateUpdateSearcher();
    uSearcher.Online = false;
    try {
        ISearchResult sResult = uSearcher.Search("IsInstalled=1 And IsHidden=0");
        textBox1.Text = "Found " + sResult.Updates.Count + " updates" + Environment.NewLine;
        foreach (IUpdate update in sResult.Updates) {
                textBox1.AppendText(update.Title + Environment.NewLine);
        }
    }
    catch (Exception ex) {
        Console.WriteLine("Something went wrong: " + ex.Message);
    }
}
Posted
Updated 27-May-16 23:26pm
v4
Comments
[no name] 27-May-16 16:50pm    
Hi, it is great that you Show what you have tried. But please also mention some Details about the result of your Trials. E.g. what you expect and what you get. This will make it much more easy to help ;)

First add a reference to WUApiLib "C:\windows\system32\Wuapi.dll"
see below snippet
C#
WUApiLib.AutomaticUpdatesClass auc = new WUApiLib.AutomaticUpdatesClass();
bool active = auc.ServiceEnabled;

MSDN says :"The ServiceEnabled property indicates whether all the components that Automatic Updates requires are available."
 
Share this answer
 
Something along the lines as this:

Enumerating and Controlling Windows Services with C#[^]
 
Share this answer
 
C#
Have you considered enumerating the services running on the machine and finding the Windows Update service as well as BITS (background intelligent transfer) and finding out their current state?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900