views:

209

answers:

4

I have a URL and I want to launch it in the default browser. I've tried two methods:

Process.Start("http://stackoverflow.com");

... and the one detailed in this other question using ShellExecute.

In both cases I get the error: Windows cannot find 'http://stackoverflow.com'. Make sure you typed the name correctly, and then try again.

It shouldn't be trying to open it as a file though... from what I understand, it should recognize it as a URL and open it in the default browser. What am I missing?

By the way: OS = Vista, and .NET = 3.5

EDIT:

According to this MS KB article, since Process.Start sets the UseShellExecute by default, it should launch the default browser.

EDIT:

Here's what does work:

System.Diagnostics.Process.Start(@"C:\Program Files\Internet Explorer\IExplore.exe", "http://stackoverflow.com");

Unfortunately that really doesn't launch the default browser, and it also doesn't work if IE isn't installed in the "normal" place. I'm not sure what to do here.

More information:

OK, so the error I'm getting is error number -2147467259. Looking at Google for this, it appears that it's not very descriptive. It might be a file association error or something.

The plot thickens:

So I checked the registry key that's supposed to have my file association for http:

KEY_CLASSES_ROOT\http\shell\open\command\default

Here's the value:

"C:\Program Files\Mozilla Firefox\firefox.exe" -requestPending -osint -url "%1"

That makes sense. I actually copied this string into a command prompt and replaced the %1 with http://stackoverflow.com and it worked and opened firefox. I just don't get why Process.Start isn't associating the URL with this command...

+3  A: 

Try

Process.Start("IExplore.exe http://www.stackoverflow.com");

This will launch Internet Explorer and the URL. Process.Start does not detect applications/browsers automaticall.y

Russell
Please see my edit
Scott Whitlock
I get the same error using your method.
Scott Whitlock
I think this is a bad practice. Don't depend on IE in such a crude way if there is an easy, better way.
mafutrct
Well the default browser location is in the registry you can retrieve the process value to start.
Russell
HKEY_CLASSES_ROOT \ http \ shell \ open \ command
Russell
@Russel: Yes, see the later part of my question. It seems to be right, and Process.Start should be using it, but it's not.
Scott Whitlock
+1  A: 

This works for me:

Process proc = new Process ();
proc.StartInfo.UseShellExecute = true;
proc.StartInfo.FileName = "http://stackoverflow.com";
proc.Start ();

Don't forget UseShellExecute if you want to use automatic recognition of command type (in this case, http/browser).

Edit: Does it work if you Win+R the url?

mafutrct
This is identical to the method I'm using. Just calling Process.Start sets UseShellExecute by default (see the KB article I linked in my question).
Scott Whitlock
Indeed, the MSDN agrees with the KB article as well. I'm confused. This has to work. Is your URL really correct? Default browser set? Does the code in this answer work or does it result in the same message? Raymond to the rescue!
mafutrct
It gives the same message (that's actually the code I tried first). I'm thinking it has to be a file association problem on my machine. I'm trying to figure out how to troubleshoot that.
Scott Whitlock
Yes, that sounds like a plausible explanation. Sorry for not thoroughly reading the question at first, btw, you explained that well. :)
mafutrct
Did you think about putting the question up on SU?
mafutrct
That's a good point. I might (btw, I added some more info, again).
Scott Whitlock
Also, yes it works fine if I use Win+R and type in the URL.
Scott Whitlock
A: 

Ok, so it mysteriously started working properly without changing anything. I can't explain it. However, in the mean time, I wrote another method of finding and executing the default browser. It's a little bit hacky, but much better than just loading IE by default:

bool success = false;
RegistryKey httpKey = Registry.ClassesRoot.OpenSubKey(@"http\shell\open\command");
if (httpKey != null && httpKey.GetValue(string.Empty) != null)
{
    string cmd = httpKey.GetValue(string.Empty) as string;
    if (cmd != null)
    {
        try
        {
            if (cmd.Length > 0)
            {
                string[] splitStr;
                string fileName;
                string args;
                if (cmd.Substring(0,1) == "\"")
                {
                    splitStr = cmd.Split(new string[] { "\" " }, StringSplitOptions.None);
                    fileName = splitStr[0] + "\"";
                    args = cmd.Substring(splitStr[0].Length + 2);
                }
                else
                {
                    splitStr = cmd.Split(new string[] { " " }, StringSplitOptions.None);
                    fileName = splitStr[0];
                    args = cmd.Substring(splitStr[0].Length + 1);
                }
                System.Diagnostics.Process.Start(fileName, args.Replace("%1","http://stackoverflow.com"));
                success = true;
            }
        }
        catch (Exception)
        {
            success = false;
        }
    }
    httpKey.Close();
}
Scott Whitlock
A: 

This is a serious issue that I saw when Firefox is the default web browser.

If we use System.Windows.Forms.Help.ShowHelp(null, "http://microsoft.com"), such error message can be worked around on Windows. However, Help.ShowHelp does not work as expected, on Mono/openSUSE.

Lex Li