tags:

views:

237

answers:

2

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.

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.

+2  A: 

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

Sky Sanders
I kinda had 2 figured out;) Is there a way to speed up 1?
dbasnett
@dbasnett - updated answer
Sky Sanders
In another place I start the default mail program withmailto:blahblahand it runs immediately.
dbasnett
winner, winner, chicken dinner. Dim brwsrKey As String = "HKEY_CURRENT_USER\Software\Classes\http\shell\open\command" Dim brwsr As String = Microsoft.Win32.Registry.GetValue(brwsrKey, "", "").ToString Dim foo() As String = brwsr.Split(New Char() {""""c}, StringSplitOptions.RemoveEmptyEntries)
dbasnett
I spoke too soon. On two other machines, one XP and One Windows 7, it did not work. No browser window appeared, or if one was open it did not browse to the specified location.
dbasnett
@dbasnett - ok, so 'works on my machine' won't fly, huh? lol. updated answer with another possibility. Let me know.
Sky Sanders
A: 

use:

System.Diagnostics.Process.Start("http://www.google.com");