How to start the default Internet browser programmatically by using Visual C#

This article was previously published under Q305703
For a Microsoft Visual Basic .NET version of this article, see 305705.
For a Microsoft Visual C++ .NET version of this article, see 307382.

This article refers to the following Microsoft .NET Framework Class Library namespace:
  • System.Diagnostics


This step-by-step article shows you how to start the default Internet browser.

back to the top


  • Microsoft Visual Studio .NET
back to the top

Specify the URL, FTP, or File to Open

You can specify a URL, a file, or an FTP address. All three of these assignments are valid:
   string target= "";   string target = "";   string target = "C:\\Program Files\\Microsoft Visual Studio\\INSTALL.HTM";  				
back to the top

Use the Process Class Start Method to Start the Browser

The Process class contains a static Start method. Because this is a static method, you can call Start without having an instance of a Process class.
back to the top

Provide Exception Handling

Because you take advantage of the default UseShellExecute property when you call the Start method, you do not have to explicitly query the registry to determine which browser is the default. However, if you use this approach on a computer that does not have a browser installed, an exception occurs. This exception must be caught so that the appropriate action can be taken.

This example explicitly traps for an error that is generated when the necessary registry key is not found and indicates that no browser is installed. Additionally, a general exception handler is provided for other errors that may occur. The try...catch block is demonstrated in the complete code listing.

back to the top

Complete Code Sample

    string target= "";    //Use no more than one assignment when you test this code.     //string target = "";    //string target = "C:\\Program Files\\Microsoft Visual Studio\\INSTALL.HTM";     try        {         System.Diagnostics.Process.Start(target);        }    catch        (          System.ComponentModel.Win32Exception noBrowser)         {         if (noBrowser.ErrorCode==-2147467259)          MessageBox.Show(noBrowser.Message);        }    catch (System.Exception other)        {          MessageBox.Show(other.Message);        }				
back to the top


This code is highly dependent on the application-file type associations in the HKEY_CLASSES_ROOT hive of the registry. This can lead to unexpected results and exceptions if the registry is damaged. Additionally, file types and extensions may be associated with applications other than the browser. For example, HTM or HTML files may be associated with Web development software instead of the browser.

back to the top


For more information about the Process class, visit the following Microsoft Web site:back to the top
launch try-catch

Article ID: 305703 - Last Review: 06/19/2012 03:18:00 - Revision: 3.0

  • Microsoft Visual C# .NET 2002 Standard Edition
  • Microsoft Visual C# 2005
  • kbhowtomaster kbprod2web KB305703