15

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...

Community
  • 1
  • 1
Scott Whitlock
  • 13,739
  • 7
  • 65
  • 114

4 Answers4

12

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?

mafu
  • 31,798
  • 42
  • 154
  • 247
  • 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 May 09 '10 at 02:11
  • 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! – mafu May 09 '10 at 02:16
  • 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 May 09 '10 at 02:17
  • Yes, that sounds like a plausible explanation. Sorry for not thoroughly reading the question at first, btw, you explained that well. :) – mafu May 09 '10 at 02:21
  • Did you think about putting the question up on SU? – mafu May 09 '10 at 02:23
  • That's a good point. I might (btw, I added some more info, again). – Scott Whitlock May 09 '10 at 02:25
  • Tks, solved my problem!! I just don't know why the "Process.Start("Url");" wanst working, any idea? – Luan Cardoso Jan 24 '21 at 18:51
1

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

Roman Marusyk
  • 23,328
  • 24
  • 73
  • 116
Russell
  • 17,481
  • 23
  • 81
  • 125
  • 3
    I think this is a bad practice. Don't depend on IE in such a crude way if there is an easy, better way. – mafu May 09 '10 at 02:09
  • Well the default browser location is in the registry you can retrieve the process value to start. – Russell May 09 '10 at 02:24
  • @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 May 09 '10 at 02:28
1

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
  • 13,739
  • 7
  • 65
  • 114
  • the solution with ProcessInfo, although more logical, doesn't always work - this one works nicely, but e.g. on Windows 7 (where the other solution doesn't work). In order to make it work on Windows 10 too (where the other solution with ProcessINfo might actually work), you should check HKCU\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http\UserChoice registry key - the value of ProgId should point to HKCR entry, instead of the default "http" above – hello_earth Mar 24 '20 at 09:31
1

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
  • 60,503
  • 9
  • 116
  • 147