Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I open a web browser from a .NET Program? Process.Start() isn't working?

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

like image 891
Scott Whitlock Avatar asked May 09 '10 01:05

Scott Whitlock


2 Answers

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?

like image 55
mafu Avatar answered Sep 23 '22 13:09

mafu


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();
}
like image 23
Scott Whitlock Avatar answered Sep 22 '22 13:09

Scott Whitlock