How to exit C# Console app if there is no updates to download or install?

0

I am busy writing a console app whereby it searches for any updates on a user pc, then downloads it and installs it. All of this works great, however when there are no updates available it tries to download and then the app crashes with this error

System.Runtime.InteropServices.COMException: '0x80240024'

I suspect that it can't find the updates to download therefore it throws this COMexception

This is the code I am using

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();  ---> Exception Thrown here!!
            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");
        }
     }
   }
 }

And it throws the exception by this line:

 IDownloadResult DownloadResult = Downloader.Download();

I have tried adding a try catch but not working.

I just basically need to tell it that when there is no update available to say "there are no updates available, please press any key to exit"

Thanks

c#
console
asked on Stack Overflow Oct 5, 2020 by Newbie

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0