84

I'm trying to provide a link to my company's website from a Windows Form. I want to be well behaved and launch using the user's preferred browser.

What is the best way to open a URL in the user's default browser from a Windows Forms application?

Matt Hamilton
  • 200,371
  • 61
  • 386
  • 320
Adrian Clark
  • 12,449
  • 5
  • 36
  • 42

7 Answers7

141

This article will walk you through it.

Short answer:

ProcessStartInfo sInfo = new ProcessStartInfo("http://mysite.com/");  
Process.Start(sInfo);
Blorgbeard
  • 101,031
  • 48
  • 228
  • 272
37
using System.Diagnostics;

Process.Start("http://www.google.com/");

This approach has worked for me, but I could be missing something important.

Aaron Wagner
  • 5,739
  • 1
  • 31
  • 38
22

For those getting a "Win32Exception: The System cannot find the file specified"

This should do the work:

ProcessStartInfo psInfo = new ProcessStartInfo
{
   FileName = "https://www.google.com",
   UseShellExecute = true
};
Process.Start(psInfo);

UseShellExecute is descriped further here

For me the issue was due to the .NET runtime as descriped here

Daniel
  • 398
  • 2
  • 16
13

Here is the best of both worlds:

Dim sInfo As New ProcessStartInfo("http://www.mysite.com")

Try
     Process.Start(sInfo)
Catch ex As Exception
     Process.Start("iexplore.exe", sInfo.FileName)
End Try

I found that the answer provided by Blorgbeard will fail when a desktop application is run on a Windows 8 device. To Camillo's point, you should attempt to open this with the user's default browser application, but if the browswer application is not assigned, an unhandled exception will be thrown.

I am posting this as the answer since it handles the exception while still attempting to open the link in the default browser.

Rogala
  • 2,679
  • 25
  • 27
6

I like approach described here. It takes into account possible exceptions and delays when launching the browser.

For best practice make sure you don't just ignore the exception, but catch it and perform an appropriate action (for example notify user that opening the browser to navigate him to the url failed).

Blorgbeard
  • 101,031
  • 48
  • 228
  • 272
Sumrak
  • 3,740
  • 7
  • 31
  • 37
  • 2
    True, and that you should always consider. Their method of just swallowing exceptions makes me cringe though. You may be able to make arguments for it in this specific case but I'd still never have an empty "catch" block. It is too easy to entirely mask an issue that way. – Adrian Clark Sep 30 '09 at 23:32
  • you're right. I didn't actually think about possible exception (and I should've) until I saw the post. – Sumrak Oct 01 '09 at 00:44
0

Normally, Process.Start method asigning the url doesnt work if the default browser is already configurated on your OS.

You can try do this:

Process.Start(new ProcessStartInfo("cmd", $"/c start {m_urlYouWantToOpen}") { CreateNoWindow = true });

I hope it works for you!

-3

The above approach is perfect, I would like to recommend this approach to where you can pass your parameters.

Process mypr;
mypr = Process.Start("iexplore.exe", "pass the name of website");
Spontifixus
  • 6,570
  • 9
  • 45
  • 63
ammrin
  • 7
  • 1
    This should be a comment, not an answer. – Austin Henley Oct 01 '12 at 13:42
  • Please use a proper writing style, and refrain from writing in "chat language" here... – Spontifixus Oct 01 '12 at 13:47
  • 1
    I was wondering why you felt this approach is superior? It seems to me that this does not to what was originally intended, which was open in the user's preferred browser. This method supplies the browser executable so would always open in IE even if the user had specified Chrome or Firefox as their preferred web browser. – Adrian Clark Oct 03 '12 at 00:16
  • 4
    -1 because of Internet Explorer. ***I hate it*** when some dumb application opens Internet Explorer, makes me cringe just waiting it to start up so I can close the damn thing and open the URL in an ***actual proper web browser***. This kind of thing is an instant reason to think the application was coded by twelve-year-olds. – Camilo Martin Feb 04 '13 at 23:37