What is the best way to launch a web browser with a custom url from a C# application?

It's common knowledge that using System.Diagnostics.Process.Start is the way to launch a url from a C# applicaiton:

System.Diagnostics.Process.Start("http://www.mywebsite.com");

However, if this url is invalid the application seems to have no way of knowing that the call failed or why. Is there a better way to launch a web browser? If not, what is my best option for url validation?

Answers


Try an approach as below.

try
{
    var url = new Uri("http://www.example.com/");

    Process.Start(url.AbsoluteUri);
}
catch (UriFormatException)
{
    // URL is not parsable
}

This does not ensure that the resource exist, but it does ensure the URL is wellformed. You might also want to check if the scheme is matching http or https.


If you need to verify that the URL exists, the only thing you can do is create a custom request in advance and verify that it works. I'd still use the Process.Start to shell out to the actual page, though.


Check the Uri.IsWellFormedUriString static method. It's cheaper than catching exception.


Need Your Help

Change Href on browser size + jQuery

javascript jquery css drop-down-menu

I have a responsive menu, when viewing on PC on a hover the menu drops, on a tablet obviously you cant hover so on click I need the menu to drop. However the link is directing the user to the page ...

Creating Ambient mode for Gear S2 watch face in Tizen SDK

sdk tizen

I would like to enable ambient mode in my standalone watch face (.wgt format) that is written in Tizen SDK for Gear S2.