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:
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.}
}
}
}
Popular Articles
Recent comments
- thank you for sharing
1 day 2 hours ago - Great explanation and more questions
2 days 5 hours ago - Insertion of illegal Element:
4 weeks 4 days ago - Insertion of illegal Element: 32
4 weeks 4 days ago - re "But, this will NOT work."
5 weeks 5 days ago - Unable to cast COM object of t
5 weeks 5 days ago - Saved my life
5 weeks 6 days ago - nice
8 weeks 5 days ago - good article
9 weeks 6 days ago - windows 2008 server backups
11 weeks 5 days ago

viewsource:
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:
try
{
Process.Start(url)
}
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...
Hi,
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
http://support.microsoft.com/kb/305703
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.
easy
string target= "http://www.microsoft.com";
//Use no more than one assignment when you test this code.
//string target = "ftp://ftp.microsoft.com";
//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);
}