none
Installing updated found using WUApiLib RRS feed

  • Question

  • I just started learning C#, and I thought a good way to start was building an assisting tool for Windows.

    I created a bunch of function and at the very end I decided to try making a windows update function. So far here is what I got (I created this code by following a guide step by step to learn what I was doing)

    using WUApiLib;

    private void button6_Click(object sender, EventArgs e) { base.OnLoad(e); UpdateSession uSession = new UpdateSession(); IUpdateSearcher uSearcher = uSession.CreateUpdateSearcher(); uSearcher.Online = true; try { ISearchResult sResult = uSearcher.Search("IsInstalled=1 And IsHidden=0"); textBox1.Text = "Found " + sResult.Updates.Count + " update(s)" + Environment.NewLine; foreach (IUpdate update in sResult.Updates) { textBox1.AppendText(update.Title + Environment.NewLine); } } catch (Exception ex) { Console.WriteLine("We got an error!: " + ex.Message); } }


    This is showing any pending updates in the machine, I tried to read more about it but I wasn't able to install the pending update, any help?

    Tuesday, January 8, 2019 11:32 AM

Answers

  • So you want to install the update?  Use the IUpdateInstaller interface for that. Note that this isn't going to be simple as you're dealing with a somewhat complicated API. If you're just learning to code then I wouldn't recommend going down this route. The documentation is here.

    Michael Taylor http://www.michaeltaylorp3.net

    • Marked as answer by TheNoobuUser Wednesday, January 9, 2019 8:23 AM
    Tuesday, January 8, 2019 3:07 PM
    Moderator

All replies

  • So you want to install the update?  Use the IUpdateInstaller interface for that. Note that this isn't going to be simple as you're dealing with a somewhat complicated API. If you're just learning to code then I wouldn't recommend going down this route. The documentation is here.

    Michael Taylor http://www.michaeltaylorp3.net

    • Marked as answer by TheNoobuUser Wednesday, January 9, 2019 8:23 AM
    Tuesday, January 8, 2019 3:07 PM
    Moderator
  • Righty thanks! I'll have a look and see how complicated is that and if it's to hard for me right now, I'll save it for later, much appreciated! 
    Wednesday, January 9, 2019 8:23 AM
  • After a while I came up with this, I'll leave it here so maybe it will be usefull to someone else

    private void button6_Click(object sender, EventArgs e)
            {
                base.OnLoad(e);
                UpdateSession uSession = new UpdateSession();
                IUpdateSearcher uSearcher = uSession.CreateUpdateSearcher();
                UpdateCollection updatesToInstall = new UpdateCollection();
                uSearcher.Online = true;
                try
                {
                    ISearchResult sResult = uSearcher.Search("IsInstalled=1 And IsHidden=0");
                    textBox1.Text = "Found " + sResult.Updates.Count + " update(s)" + Environment.NewLine;
                    foreach (IUpdate update in sResult.Updates)
                    {
                        textBox1.AppendText(update.Title + Environment.NewLine);
                        if (update.IsDownloaded)
                            updatesToInstall.Add(update);
                    }
    
                    IUpdateInstaller installer = uSession.CreateUpdateInstaller();
                    installer.Updates = updatesToInstall;
                    IInstallationResult installationRes = installer.Install();
    
                    for (int i = 0; i < updatesToInstall.Count; i++)
                    {
                        if (installationRes.GetUpdateResult(i).HResult == 0)
                        {
                            textBox1.AppendText("Installed : " + updatesToInstall[i].Title);
                        }
                        else
                        {
                            textBox1.AppendText("Failed : " + updatesToInstall[i].Title);
                        }
                    }
                }
                catch (Exception ex)
                {
                    textBox1.AppendText("We got an error!: " + ex.Message);
                }
            }

    Wednesday, January 9, 2019 12:55 PM