Open a url in default browser using VB.Net

Have you ever wanted to link to your website from your VB.Net program and wondered how to automatically open user’s default browser and navigate to your website. If so, it just a matter of a single line! Here is it:

But of course, you could do a lot more..

The Process class resides in the System.Diagnostics Namespace. The Process.Start() function is declared as

and a little more which is beyond our topic. You could refer it here : http://msdn.microsoft.com/en-us/library/system.diagnostics.process.start

 

Specifying the browser

You can easily specify which browser to open by invoking the 2 argument function.

and so on.

Using the ProcessStartInfo

You could alternatively use the ProcessStartInfo as the Parameter to Process.Start() function

Catching Exceptions

Sometimes, If the specified browser is not installed on the user’s machine or browser was not opened for any other case, the Process.Start() function throws a Win32Exception which should be caught.

for example,

Process.Start() function throws few more Exceptions such as InvalidOperationException, ObjectDisposedException, FileNotFoundException, ArgumentNullException. So it is a good idea to catch all Exceptions at once.

What else

What else you can do to polish it? Here are a few ideas:

  • Check whether user is connected to internet before opening the browser
  • Monitor the started process by using handle returned by Process.Start()

If you feel i’ve missed something, comment it below and thanks in advance 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *