C#WUApiLib:即使找到更新,也无法使用0x80240024安装更新

问题描述

我遵循了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 );
        }
    }
}

解决方法

原来这是特权问题。我以管理员身份运行,并且运行正常。