【问题标题】:C# WUApiLib: Failing to Install Updates with 0x80240024 Even Though Updates are FoundC# WUApiLib:即使找到更新,也无法使用 0x80240024 安装更新
【发布时间】:2020-09-25 00:11:25
【问题描述】:

我已按照this site 上的说明进行操作,这是我在研究Stack Overflow and found this question 时发现的。但是,我似乎无法弄清楚为什么我的代码会碰到 catch 块。我在 IInstallationResult 行有一个中断,代码命中了该行,但随后进入了捕获状态,指出 0x80240024 (There are no updates),即使肯定有可用的更新并且我在文本框中列出了它们。

我在这里错过了什么?

using System;
using System.Windows.Forms;
using WUApiLib;

namespace TEST.DetectWindowsUpdate
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void GetUpdates(bool downloadUpdates, bool installUpdates)
        {

            UpdateSession uSession = new UpdateSession();
            IUpdateSearcher uSearcher = uSession.CreateUpdateSearcher();
            UpdateCollection updatesToInstall = new UpdateCollection();
            UpdateDownloader downloader = uSession.CreateUpdateDownloader();
            IUpdateInstaller installer = uSession.CreateUpdateInstaller();

            uSearcher.Online = true;

            try
            {
                ISearchResult sResult = uSearcher.Search("IsInstalled=0 And Type='Software'");
                lblUpdateCount.Text = sResult.Updates.Count.ToString();

                foreach (IUpdate update in sResult.Updates)
                {
                    txtUpdatesList.AppendText("Title: " + update.Title + Environment.NewLine);
                    txtUpdatesList.AppendText("IsInstalled: " + update.IsInstalled.ToString() + Environment.NewLine);
                    txtUpdatesList.AppendText("Downloaded: " + update.IsDownloaded.ToString() + Environment.NewLine);
                    txtUpdatesList.AppendText("IsMandatory: " + update.IsMandatory.ToString() + Environment.NewLine);

                    txtUpdatesList.AppendText(Environment.NewLine);

                    if (downloadUpdates)
                    {
                        if (update.IsDownloaded == false)
                        {
                            do
                            {
                                downloader.Updates.Add(update);
                                downloader.Download();
                            }
                            while (update.IsDownloaded == false);

                            if (update.IsDownloaded == true)
                            {
                                updatesToInstall.Add(update);
                            }
                        }
                        else
                        {
                            updatesToInstall.Add(update);
                        }
                    }

                    if (installUpdates)
                    {
                        installer.Updates = updatesToInstall;

                        IInstallationResult installationRes = installer.Install();

                        for (int i = 0; i < updatesToInstall.Count; i++)
                        {
                            txtUpdatesList.AppendText("Installing " + updatesToInstall[i].Title + "...");
                            if (installationRes.GetUpdateResult(i).HResult == 0)
                            {
                                txtUpdatesList.AppendText("INSTALL SUCCESS FOR " + updatesToInstall[i].Title);
                            }
                            else
                            {
                                txtUpdatesList.AppendText("INSTALL FAIL FOR " + updatesToInstall[i].Title);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error: " + ex.Message + ": " + ex.HelpLink);
            }
        }

        private void btnGetUpdates_Click(object sender, EventArgs e)
        {
            
            base.OnLoad(e);
            txtUpdatesList.Clear();
            GetUpdates(false, false);
        }

        private void btnInstallUpdates_Click(object sender, EventArgs e)
        {
            base.OnLoad(e);
            txtUpdatesList.Clear();
            GetUpdates(true, true );
        }
    }
}

【问题讨论】:

    标签: c# windows-update


    【解决方案1】:

    原来是权限问题。我以管理员身份运行,它运行正常。

    【讨论】:

      猜你喜欢
      • 2018-08-22
      • 1970-01-01
      • 1970-01-01
      • 2013-03-12
      • 1970-01-01
      • 2017-12-08
      • 2021-01-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多