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

后端 未结 6 1966
鱼传尺愫
鱼传尺愫 2021-01-14 19:00

I\'m specifically interested in Windows 2000/XP, but Vista/7 would be interesting too (if different).

I was thinking along the lines of task scheduling a batch file

6条回答
  •  春和景丽
    2021-01-14 19:52

    You could use WUApiLib:

    UpdateSessionClass session = new UpdateSessionClass();
    
    IUpdateSearcher search = session.CreateUpdateSearcher();
    
    ISearchResult result = search.Search("IsInstalled=0 and IsPresent=0 and Type='Software'");
    
    int numberOfUpdates = result.Updates.Count - 1;
    
    Log.Debug("Found " + numberOfUpdates.ToString() + " updates");
    
    UpdateCollection updateCollection = new UpdateCollection();
    
    for (int i = 0; i < numberOfUpdates; i++)
    {
        IUpdate update = result.Updates[i];
    
        if (update.EulaAccepted == false)
        {
            update.AcceptEula();
        }
    
        updateCollection.Add(update);
    }
    
    if (numberOfUpdates > 0)
    {
        UpdateCollection downloadCollection = new UpdateCollection();
    
        for (int i = 0; i < updateCollection.Count; i++)
        {
            downloadCollection.Add(updateCollection[i]);
        }
    
        UpdateDownloader downloader = session.CreateUpdateDownloader();
    
        downloader.Updates =  downloadCollection;
    
        IDownloadResult dlResult = downloader.Download();
    
        if (dlResult.ResultCode == OperationResultCode.orcSucceeded)
        {
            for (int i = 0; i < downloadCollection.Count; i++)
            {
                Log.Debug(string.Format("Downloaded {0} with a result of {1}", downloadCollection[i].Title, dlResult.GetUpdateResult(i).ResultCode));
            }
    
            UpdateCollection installCollection = new UpdateCollection();
    
            for (int i = 0; i < updateCollection.Count; i++)
            {
                if (downloadCollection[i].IsDownloaded)
                {
                    installCollection.Add(downloadCollection[i]);
                }
            }
    
            UpdateInstaller installer = session.CreateUpdateInstaller() as UpdateInstaller;
    
            installer.Updates = installCollection;
    
            IInstallationResult iresult = installer.Install();
    
            if (iresult.ResultCode == OperationResultCode.orcSucceeded)
            {
                updated = installCollection.Count.ToString() + " updates installed";
    
                for (int i = 0; i < installCollection.Count; i++)
                {
                    Log.Debug(string.Format("Installed {0} with a result of {1}", installCollection[i].Title, iresult.GetUpdateResult(i).ResultCode));
                }
    
                if (iresult.RebootRequired == true)
                {
                    ManagementClass mcWin32 = new ManagementClass("Win32_OperatingSystem");
    
                    foreach (ManagementObject shutdown in mcWin32.GetInstances())
                    {
                        shutdown.Scope.Options.EnablePrivileges = true;
                        shutdown.InvokeMethod("Reboot", null);
                    }
                }
            }
    

提交回复
热议问题