【问题标题】:windows update applicationWindows 更新应用程序
【发布时间】:2011-10-08 13:00:41
【问题描述】:

我在这里编写了一个使用 WUA COM 接口(@98​​7654321@、IUpdate 等)的应用程序。我使用此应用程序扫描可用更新、下载更新并安装它们。一切正常,直到我下载并安装一些更新,并有一些 ui 更新对话框。

我在使用IUpdateSearcher.Search() 时获得此更新,我可以成功下载它(使用IUpdateDownloader.Download())但是当我使用IUpdateInstaller2.Install() 安装此更新时,我无法摆脱用户界面。

我的问题是 - 如何进行静默安装?

【问题讨论】:

  • 只是出于好奇,请问您为什么要编写这样的应用程序?
  • 它是大系统的一部分,可以控制多台电脑......(其中一个选项是控制 eche PC 的 Windows 更新......)
  • 找到解决方案使用IUpdateInstaller2 instend IUpdateInstaller,有“forcesielte”属性...

标签: c# .net msdn windows-update


【解决方案1】:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WUApiLib;
namespace MSHWindowsUpdateAgent
{
    class Program
    {
        static void Main(string[] args)
        {
            UpdatesAvailable();
            EnableUpdateServices();//enables everything windows need in order to make an update
            InstallUpdates(DownloadUpdates());
            Console.Read();
        }
        //this is my first try.. I can see the need for abstract classes here...
        //but at least it gives most people a good starting point.
        public static  void InstalledUpdates()
        {
            UpdateSession UpdateSession = new UpdateSession();
            IUpdateSearcher UpdateSearchResult = UpdateSession.CreateUpdateSearcher();
            UpdateSearchResult.Online = true;//checks for updates online
            ISearchResult SearchResults = UpdateSearchResult.Search("IsInstalled=1 AND IsHidden=0");
            //for the above search criteria refer to 
            //http://msdn.microsoft.com/en-us/library/windows/desktop/aa386526(v=VS.85).aspx
            //Check the remakrs section

            foreach (IUpdate x in SearchResults.Updates)
            {
                Console.WriteLine(x.Title);
            }
        }
        public static void UpdatesAvailable()
        {
            UpdateSession UpdateSession = new UpdateSession();
            IUpdateSearcher UpdateSearchResult = UpdateSession.CreateUpdateSearcher();
            UpdateSearchResult.Online = true;//checks for updates online
            ISearchResult SearchResults = UpdateSearchResult.Search("IsInstalled=0 AND IsPresent=0");
            //for the above search criteria refer to 
            //http://msdn.microsoft.com/en-us/library/windows/desktop/aa386526(v=VS.85).aspx
            //Check the remakrs section

            foreach (IUpdate x in SearchResults.Updates)
            {
                Console.WriteLine(x.Title);
            }
        }
        public static UpdateCollection DownloadUpdates()
        {
            UpdateSession UpdateSession = new UpdateSession();
            IUpdateSearcher SearchUpdates = UpdateSession.CreateUpdateSearcher();
            ISearchResult UpdateSearchResult = SearchUpdates.Search("IsInstalled=0 and IsPresent=0");
            UpdateCollection UpdateCollection = new UpdateCollection();
            //Accept Eula code for each update
            for (int i = 0; i < UpdateSearchResult.Updates.Count; i++)
            {
                IUpdate Updates = UpdateSearchResult.Updates[i];
                if (Updates.EulaAccepted == false)
                {
                    Updates.AcceptEula();
                }
                UpdateCollection.Add(Updates);
            }
            //Accept Eula ends here
            //if it is zero i am not sure if it will trow an exception -- I havent tested it.

                UpdateCollection DownloadCollection = new UpdateCollection();
                UpdateDownloader Downloader = UpdateSession.CreateUpdateDownloader();

                for (int i = 0; i < UpdateCollection.Count; i++)
                {
                    DownloadCollection.Add(UpdateCollection[i]);
                }

                Downloader.Updates = DownloadCollection;
                Console.WriteLine("Downloading Updates");
                IDownloadResult DownloadResult = Downloader.Download();
                UpdateCollection InstallCollection = new UpdateCollection();
                for (int i = 0; i < UpdateCollection.Count; i++)
                {
                    if (DownloadCollection[i].IsDownloaded)
                    {
                        InstallCollection.Add(DownloadCollection[i]);
                    }
                }
                return InstallCollection;
        }
        public static void InstallUpdates(UpdateCollection DownloadedUpdates)
        {
            UpdateSession UpdateSession = new UpdateSession();
            UpdateInstaller InstallAgent = UpdateSession.CreateUpdateInstaller() as UpdateInstaller;
            InstallAgent.Updates = DownloadedUpdates;

            //Starts a synchronous installation of the updates.
            // http://msdn.microsoft.com/en-us/library/windows/desktop/aa386491(v=VS.85).aspx#methods
            IInstallationResult InstallResult = InstallAgent.Install();

        }
        public static void EnableUpdateServices()
        {
            IAutomaticUpdates updates = new AutomaticUpdates();
            if (!updates.ServiceEnabled)
            {
                Console.WriteLine("Not all updates services where enabled. Enabling Now" + updates.ServiceEnabled);
                updates.EnableService();
                Console.WriteLine("Service enable success");
            }


        }

    }
}

【讨论】:

    【解决方案2】:

    jvelez 有一个很好的答案,但它没有直接回答实际问题:“我怎样才能使它成为一个静默安装?”

    confi - 我首先建议最简单的方法:

    static class Program
    {
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
    Application.SetCompatibleTextRenderingDefault(false);
    Application.EnableVisualStyles();
    
    Thread thread = new Thread(() =>
    {
    Form1 form1 = new Form1();
    form1.Visible = false;
    form1.ShowInTaskbar = false;
    Application.Run(form1);
    });
    
    thread.SetApartmentState(ApartmentState.STA);
    thread.Start();
    }
    }
    

    通过简单地隐藏您的 UI 来确保所有方法都在后台运行,这将为您提供一种使其静默的方法。

    @Justin Choponis - 我需要做和confi一样的事情。 WSUS 并没有为我的许多客户削减它。它不切实际且笨重。如果您有一个网络需要照顾并且一直在现场,那么 WSUS 会很有用,但我觉得它非常不令人满意。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多