Use this function to retrieve path of default browser
/// <summary>
/// Reads path of default browser from registry
/// </summary>
/// <returns></returns>
private static string GetDefaultBrowserPath()
{
string key = @"htmlfile\shell\open\command";
RegistryKey registryKey =
Registry.ClassesRoot.OpenSubKey(key, false);
// get default browser path
return ((string) registryKey.GetValue(null, null)).Split('"')[1];
}
Opens URL in default browser from within the C# program.
string defaultBrowserPath = GetDefaultBrowserPath();
try
{
// launch default browser
Process.Start(defaultBrowserPath, "http://www.yahoo.com");
}
catch (Exception exp)
{
MessageBox.Show(exp.Message);
}
Opens URL in separate instance of default browser from within the C# program.
// open URL in separate instance of default browser
Process p = new Process();
p.StartInfo.FileName = GetDefaultBrowserPath();
p.StartInfo.Arguments = "http://www.yahoo.com";
p.Start();
10 comments:
In theory, this should work, but it doesn't and it's not guaranteed to, because Firefox (at least) doesn't change the parameters for htmlfile in the registry (since the web is a lot more than just HTML). It modifies HTTP\shell\open\command though, and so does Internet Explorer, so if you want to use the default browser, you'd probably be better to look there. Opera uses this also, I believe.
Heh, I dont know why you guys making all the fuss....opening a web page in default browser is as simple as:
System.Diagnostics.Process.Start("http://www.yahoo.com");
Check out msdn article:
http://support.microsoft.com/kb/305703
Regards, Uros
How about someone decided that he wants to open all HTML files in notepad?
Better retrieve the default browser executable by the application that handles the http protocol (browser).
This can be found in:
HKEY_CLASSES_ROOT\http\shell\open\command
btw, System.Diagnostics.Process.Start("http://.... indeed works perfect for opening an webpage.
Regards, JV.
Use System.Diagnostics.Process.Start(url) throw an error if the webrowser isnt opened already
System.Diagnostics.Process.Start(url) works without the browser beeing already opened(on my system). But this works only in .NET Framework 2.0 or higher. In version 1.1 throws Win32Exception with message "The system cannot find the file specified"
Visual Studio 2002 Using .NET 1.0
work flawlessy with/without browser running.
In fact it simply ask the OS to open an URL (NOT AN HTM(L) FILE!!!!), using the default behaviour, anything else is only f***ed up windows config and not a framework error/bug.
Cheers
There is a little bug here. It does not work with FireFox.
FireFox appears in the registry like this:
C:\PROGRA~1\MOZILL~1\FIREFOX.EXE -requestPending -osint -url "%1"
While ie like this:
"C:\Program Files\Internet Explorer\IEXPLORE.EXE" -nohome
The solution here (splitting by '"') does not work with FireFox.
Here is a solution:
private static string GetDefaultBrowserPath()
{
string key = @"http\shell\open\command";
Microsoft.Win32.RegistryKey registryKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(key, false);
// get default browser path
return StringToPath(((string)registryKey.GetValue(null, null)));
}
private static string StringToPath(string str)
{
char chPathEnd = ' ';
int diff = 0;
if (str[0] == '"')
{
chPathEnd = '"';
diff = 1;
}
for (int i = 1; i < str.Length; i++)
if (str[i] == chPathEnd)
return str.Substring(diff, i - diff);
return str;
}
If you want to open the url in Internet Explorer then you can use the next code:
System.Windows.Forms.WebBrowser IE1 = new System.Windows.Forms.WebBrowser();
IE1.Visible = true;
IE1.Navigate(new System.Uri("http://www.yahoo.com"));
Hi guys,
I have one problem related to this. I have one link in windows form. When i click on that it opens in IE7 browser. But when i go to same form and click on same link it opens that link in another tab of IE. It should not go to another tab. It should show same page which i opened 1st time. Here i am working on windows application. I have used System.Diagnostics.Process.Start method. I am facing this problem. Please help me in regarding this soon as possible.
Thanks in advance..
A major problem is opening an HTML with an anchor inside it: myfile.html#myanchor
This is when looking in the registry is very useful.
Post a Comment