Reply to comment
Launch URL in Default Browser using C#
After deploying an application using the standard way of launching a URL in the default browser:
System.Diagnostics.Process.Start("http://www.google.com");We found that almost randomly, an exception is thrown on certain user's machines when this method is called. I did some research and a couple of interesting posts came up.
http://blog.benhall.me.uk/2007/12/processstart-and-first-time-launching.html
This describes the seeminly random exception in more detail. It turns out that the "System.ComponentModel.Win32Exception was unhandled" exception can actually occur when you have Firefox as your default browser but you have not run Firefox yet. So he recommends you just catch this exception and ignore it.
http://www.musicalnerdery.com/net-programming/opening-a-new-instance-of-the-users-default-browser-reliably.html
This goes into more detail on why the error occurs and provides a secondary / fail-safe to get a browser launched no matter what error occurs.
I've taken the code from these 2 articles and refactored it a bit into a method that will attempt every way possible to launch a browser with the URL specified.
public void OpenLink(string sUrl)
{ try {System.Diagnostics.Process.Start(sUrl);
}
catch(Exception exc1) { // System.ComponentModel.Win32Exception is a known exception that occurs when Firefox is default browser. // It actually opens the browser but STILL throws this exception so we can just ignore it. If not this exception, // then attempt to open the URL in IE instead.if (exc1.GetType().ToString() != "System.ComponentModel.Win32Exception")
{ // sometimes throws exception so we have to just ignore // this is a common .NET bug that no one online really has a great reason for so now we just need to try to open // the URL using IE if we can. try {System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo("IExplore.exe", sUrl);
System.Diagnostics.Process.Start(startInfo);
startInfo = null;}
catch (Exception exc2) { // still nothing we can do so just show the error to the user here.}
}
}
}
Reply
Popular Articles
Last viewed:
- Get the list of ODBC data source names programatically using C#
- SQL Server Database Project and Database Source Control
- C# Download File with Progress Bar
- Install Windws 2003 Terminal Service Licenses (Remote Desktop)
- SQL Create Table Add Description to Column
- Windows Server 2003 Configure RRAS (Routing and Remote Access Service) Site to Site VPN

Recent comments
1 day 19 hours ago
6 days 9 hours ago
6 days 9 hours ago
1 week 18 hours ago
1 week 2 days ago
2 weeks 8 hours ago
2 weeks 1 day ago
2 weeks 4 days ago
2 weeks 6 days ago
2 weeks 6 days ago