Need a simple CRM and Project Management system?
Check out JobNimbus - CRM for Contractors and Service Professionals.

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)
    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.
                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.

You should discuss the

You should discuss the competition comparison of the web log. you'll highlight it's remarkable. Your web log exploration/tour can broaden your conversions. Agen Bola Sbobet

I am truly inspired by this

I am truly inspired by this online journal! Extremely clear clarification of issues is given and it is open to every living soul. I have perused your post, truly you have given this extraordinary informative data about it. Ibcbet

Its a great pleasure reading

Its a great pleasure reading your post.Its full of information I am looking for and I love to post a comment that "The content of your post is awesome" Great work Agen Bola


i want view source of the page using c#..above code just view the page

There are a few problems with

There are a few problems with the code. It will mask "critical" exceptions, try writing:

catch (Win32Exception)
// alternate action

At very least, write
if (!(exc1 is Win32Exception))

The .GetBype().ToString() != "System.ComponentModel.Win32Exception" is extremely painful to see!

It may be painful but it is more declarative

I try to wrote the code when I post so it is really easy to understand. So I don't use short hand or less declarative methods in my code samples.

Exception handling...

Looked at your code, I believe you are trying to handle a specific scenario here. Exception handling itself is to handle cases which are NOT expected usually. So, a non-Win32 Exception does NOT guarantee everyuthing else is okay. I believe MS code in

is good enough. We simply exit gracefully if we cant handle a known exception.

I don't understand

Aren't both pure expressions without side effects? If you don't mean declarative in the sense that it has in the context of programming, but something strange like, "reads more like you might announce it in english, if you were so inclined," I would suggest that your criterion is not going to help you write code that other people can read. It's not likely to help you write what you mean, either.

Though "is" uses, from what I understand, metadata accessible through GetType(), it is most definitely not a string comparison on the type name. If the comparison was on the fully qualified type name including the full assembly string, it would be getting close to the actual semantics of "is". Anyway, definitely not short and long hands of the same thing, in case anyone cares.

I don't do this very often. Reading rats nest code all week's got me feeling like a wrangling teenager.


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";

System.ComponentModel.Win32Exception noBrowser)
if (noBrowser.ErrorCode==-2147467259)
catch (System.Exception other)