How to know if windows update is waitng for instal

2019-08-16 07:34发布

问题:

Possible Duplicate:
Best way of detecting if Windows has Windows Updates ready to download/install?

I am using C# .net 3.5.

How can I know if there are updates ready to be installed in Windows Update?

On windows 8, when Windows Update is waiting to install updates, the sleep option is disabled.

Instead of the regular 3 options: 1. Sleep 2. Restart 3. Shutdown, there are only 2 options: 1. Restart and update 2. Shutdown and update. I need to identify this state and notify the user that the machine cannot move to sleep mode becuase updates are waiting to be installed.

Can I do it using WUAPILib?

Thanks

回答1:

You can use WUApiLib (Com lib) for this:

var updateSession = new UpdateSession();
var updateSearcher = updateSession.CreateUpdateSearcher();
updateSearcher.Online = false; //set to true if you want to search online
try
{
    var searchResult = updateSearcher.Search("IsInstalled=0 And IsHidden=0");
    if (searchResult.Updates.Count > 0)
    {
        MessageBox.Show("There are updates available for installation");
    }
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message,"Error");
}

Click here if you want to know more.