0

In my console app that I am developing it displays the installed updates just fine and then it searches for available updates.

If there are no available updates it then needs to let the user know that there are no available updates and then to press any key to exit. If there are then it needs to download the updates and install (that code works as well btw)

I just need assistance in displaying a message like No New Updates Were Found and `Press Any Key To Exit"

This is what I am tryin now but not working:

public static void UpdatesAvailable()
    {
        Console.WriteLine("\nChecking for new Updates.....");
        UpdateSession UpdateSession = new UpdateSession();
        IUpdateSearcher UpdateSearchResult = UpdateSession.CreateUpdateSearcher();
        UpdateSearchResult.Online = true;//checks for updates online
        ISearchResult SearchResults = UpdateSearchResult.Search("IsInstalled=0 AND IsPresent=0");
        Console.WriteLine("\nAVAILABLE UPDATES");
        if (SearchResults != null)
        {
            Console.WriteLine("No Updates Available");
            Console.WriteLine("Press any key to exit...");
            Console.ReadKey();
        }
        else
        {
            foreach (IUpdate x in SearchResults.Updates)
            {
                Console.WriteLine(x.Title);
            }
        }

    }

I am testing on my PC and I know all my updates are installed so it does not find any new updates.

When I test on a other pc that needs updates it then displays a list of the new updates.

Thanks

  • If SearchResults == null, the foreach can never be executed because of a Null pointer exception. Maybe you have to check x. But x can also not be null if you can access Console.WriteLine(x.Title); – Markus Meyer Oct 06 '20 at 07:14
  • You want to check for SearchResults before looping through the updates. – Fabulous Oct 06 '20 at 07:29
  • @Fabulous I am checking the SearchResults now before looping through the updates. What seems to happen now is it displays "No Updates Available" and when I press any key it then displays the available updates? I have updated my question with the code I am using now –  Oct 06 '20 at 08:09
  • You have to put the foreach in the else block – Markus Meyer Oct 06 '20 at 08:23
  • @MarkusMeyer I have tried that and it still doesn't seem to work. –  Oct 06 '20 at 09:43
  • What is the current outcome. If the foreach is in the else block, it cannot happen that the updates are displayed. – Markus Meyer Oct 06 '20 at 10:21
  • @MarkusMeyer It displays the message that no updates are available. However when I remove that piece of code it then displays the updates available. –  Oct 06 '20 at 10:40
  • Does your code look like this one: https://gist.github.com/MarkusMeyer13/758b9d782a1d1cb3881d4235c8c41a98 – Markus Meyer Oct 06 '20 at 17:15
  • Excactly like that. And when I run it on a machine that's up to date with the updates its then correct. When I run it on a machine that needs updates it still says "No updates available" However when I remove that code it then picks up the updates? In theory it should work but it doesn't. –  Oct 07 '20 at 05:49
  • I have updated my question to the code I am using now –  Oct 07 '20 at 06:07
  • @MarkusMeyer I have found the solution here on SO:https://stackoverflow.com/questions/7639439/using-wua-remotely-using-c-sharp –  Oct 07 '20 at 07:06

0 Answers0