Start Default Browser - Windows
When starting the default browser like this:
Dim trgt1 As String = "http://www.vbforums.com/showthread.php?t=612471"
pi.FileName = trgt1
System.Diagnostics.Process.Start(pi)
It takes about 40 seconds to open the page.
开发者_StackOverflow中文版If I do it like this, though this isn't the default browser
Dim trgt1 As String = "http://www.vbforums.com/showthread.php?t=612471"
pi.Arguments = trgt1
pi.FileName = "iexplore.exe" 'or firefox.exe
System.Diagnostics.Process.Start(pi)
it opens immediately. Is this a bug or a feature? I have tried this with both IE and FireFox set to be the default browser.
1
Windows is running through the registry looking for an appropriate application to open the document with (via explorer.exe).
2
You are explicitly telling windows to use xxx.exe to open the document.
Update for the moving target: ;-)
The reason it is so slow is that the Url you are specifying doesn't look like anything it knows how to open, with a browser or otherwise, and has to employ brute force in determining this.
If you wan to speed up launching with the default browser, get it from HKEY_CURRENT_USER\Software\Classes\http\shell\open\command
and use #2.
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();
From this blog post
I respectfully differ with the Sky. I have tried this on numerous machines now and the value
string key = @"htmlfile\shell\open\command";
seems to always default to IE even if Chrome is set to the default browser. Now, to be honest I have not tried this on machines with firefox set to the default browser only chrome, so it could do with more testing but the value does seem to only store IE from my testing.
Hope this helps those who use alternative browsers.
I am going to stick with process.start(url)
as that pretty much guarantees that you get the users default browser every time. Let the framework handle it! that is why MS built it...
精彩评论