2

In earlier versions of Windows (before 8 and 10). You could simply execute this piece of code:

class Program
{
    static void Main(string[] args)
    {
        TryStart("myapp:", "https://www.google.com");
    }

    private static void TryStart(String url, String raw = "")
    {
        try
        {
            if (!String.IsNullOrEmpty(url))
            {
                Process.Start(url);
            }
        }
        catch
        {
            if (!String.IsNullOrEmpty(raw))
            {
                Process.Start(raw);
            }
        }
    }
}

And it would work perfectly. If the program didn't exist, it would open the webpage. If the program did exist, it would open it.

However, in Windows 8 and 10, if the program doesn't exist, you will get this message instead:

enter image description here

And the website would never be opened. Is there another way around this?

Rafael
  • 727
  • 13
  • 30
  • What is the required outcome? Opening a specific browser or specific app? Or maybe just url? Because what you are describing is the default outcome in Win 10, and it makes sense. You are trying to open an app, not start a browser. – jitendragarg Jun 22 '17 at 14:26
  • You didn't read the problem. "If the program didn't exist, it would open the webpage. If the program did exist, it would open it." It works on Windows 7, because there wasn't an app store built in to it back then. – Rafael Jun 22 '17 at 14:28
  • In windows 7, if you tried to open a process/protocol that didn't exist, you would be thrown an exception, so the snippet above would open the webpage instead. However, the exception is never thrown on Windows 8 and 10 because Microsoft circumvented the issue by redirecting to that app store pop-up. – Rafael Jun 22 '17 at 14:32
  • i'd go with 'how to detect browser protocol handlers' and handle it properly https://stackoverflow.com/questions/836777/how-to-detect-browsers-protocol-handlers – Stavm Jun 22 '17 at 14:34
  • @Stavm It's not a browser protocol, it's a process protocol. – Rafael Jun 22 '17 at 14:35
  • @Sven you are correct, but the intention remains the same, the process protocol is simply a registry value, i'd verify that value's existence prior to launch instead of hoping to catch an exception that may or may not be thrown by Windows. this way you can verify the process which is invoked as well. – Stavm Jun 22 '17 at 14:37
  • @Stavm Your answer provided is in javascript. Not C#. And no, that's an outdated answer, Windows 8 and 10 behave differently when detecting non-existing applications than Windows 10. Trying to execute a process protocol will just redirect to the App store. – Rafael Jun 22 '17 at 14:41

2 Answers2

1

How about just an idea to check if the process exist and launch appropriate program as follows:

        private static void TryStart(String url, String raw = "")
        {
            var processes = Process.GetProcessesByName(url);
            if (processes!=null && processes.Any())
            {
                Process.Start(url);

                //Process.Start(processes.First().ProcessName); //This can be used as well to start.
            }
            else
            {
                Process.Start(raw);
            }

        }
Siva Gopal
  • 3,474
  • 1
  • 25
  • 22
  • This doesn't cover if the process is not started. It will just open the webpage. – Rafael Jun 22 '17 at 14:47
  • AFAIK processes are available to query when they are started. If you want to query registered services, you can refer [MSDN](https://msdn.microsoft.com/en-us/library/hde9d63a(v=vs.110).aspx) and [this SO](https://stackoverflow.com/questions/842533/in-c-sharp-how-do-i-query-the-list-of-running-services-on-a-windows-server). A nice explanation of these aspects are provided in [this SO](https://stackoverflow.com/questions/20192531/what-the-difference-between-a-windows-service-and-a-windows-process) – Siva Gopal Jun 22 '17 at 14:55
0

Okay, I have tried to run below code myself and it works in Win 10 pro. Check it out and see if this works for you.

string url = "http://stackoverflow.com";

var processes = Process.GetProcessesByName(url);
if (processes != null && processes.Any())
{
  Process.Start(processes.First().ProcessName); //This can be used as well to start.
}
else
{
 Process.Start(url);
}

It worked when I tried to run a registered app, even if the app was not running. And if I use url, it opens default browser.

jitendragarg
  • 945
  • 1
  • 14
  • 54