C# WUApiLib know if a windows update needs a restart

1.3k views Asked by At

I use this code to get pending windows updates and also most of the informations of the update:

 static List<PendingUpdate> GetPendingUpdates()
    {
        var updateSession = new UpdateSession();
        var updateSearcher = updateSession.CreateUpdateSearcher();
        updateSearcher.Online = false; //set to true if you want to search online

        List<PendingUpdate> pendingUpdates = new List<PendingUpdate>();
        try
        {
            var searchResult = updateSearcher.Search("IsInstalled=0 And IsHidden=0");
            if (searchResult.Updates.Count > 0)
            {
                Console.WriteLine("There are updates available for installation");

                foreach (IUpdate windowsUpdate in searchResult.Updates)
                {
                    PendingUpdate update = new PendingUpdate();
                    update.Title = windowsUpdate.Title;
                    update.Description = windowsUpdate.Description;
                    update.Downloaded = windowsUpdate.IsDownloaded;
                    update.Urls = new List<string>();
                    foreach (string url in windowsUpdate.MoreInfoUrls)
                    {
                        update.Urls.Add(url);
                    }
                    foreach (dynamic category in windowsUpdate.Categories)
                    {
                        update.Categories += category.Name + ", ";
                    }
                    pendingUpdates.Add(update);
                }
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("ERROR");
            throw ex;
        }

        return pendingUpdates;
    }

I also use this code to get to know if the computer currently needs a restart to finish installed updates:

 static bool needsRestart()
    {
        ISystemInformation systemInfo = new SystemInformation();
        return systemInfo.RebootRequired;
    }

Now my question is, is it possible to get to know if an pending update needs a computer restart to finish? In the first code I get a IUpdate object but I dont see informations about a needed restart after installing this update. I there a way to get this information?

1

There are 1 answers

0
marsh-wiggle On

For the asynchronous installation I use something like this:

rebootRequired = false;

UpdateSession updateSession = new UpdateSession();
updateSession.ClientApplicationID = SusClientID;

IUpdateInstaller updatesInstaller = updateSession.CreateUpdateInstaller();
IInstallationJob job = updatesInstaller.BeginInstall(InstallProgressCallback, installComplete, installState);

// here is your installer code and the checking if the installation is completed

IInstallationProgress jobProgress = job.GetProgress();

for (int updateindex = 0; updateindex < updatesInstaller.Updates.Count; updateindex++)
{
    IUpdateInstallationResult updateInstallResult = jobProgress.GetUpdateResult(updateindex);

    rebootRequired |= updateInstallResult.RebootRequired;
}

if(rebootRequired)
{
    // any of the updates need a reboot
}