Launch URL in Default Browser using C#
After deploying an application using the standard way of launching a URL in the default browser:
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.
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.
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)
// 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.
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo("IExplore.exe", sUrl);
startInfo = null;
catch (Exception exc2)
// still nothing we can do so just show the error to the user here.
- ASP.NET 301 Redirect for Search Engine Compliant Redirects
- Dell Inspiron Mini 9 Upgrade Flash Hard Drive
- Microsoft Word Spell Checker Doesn't Check Cut and Pasted Text
- C# Free Component to Generate PDF - Convert HTML to PDF
- Performance benchmarks for LINQ vs. SqlDataReader, DataSet - LINQ Compiled Queries: Part 2
- Building a Web Service in ASP.NET 3.5
1 day 22 hours ago
- project all working Now1
3 days 21 hours ago
- Non-Clustered Index
5 days 6 hours ago
- CARA DE CHOTA
1 week 1 day ago
- This is great. Works well for
1 week 3 days ago
- thanks for jscript button disabled
1 week 6 days ago
- Reply to comment - Code Samples & Tutorials
2 weeks 5 days ago
- Wrote as an extension
3 weeks 1 day ago
4 weeks 5 days ago
4 weeks 5 days ago