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...
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:
This works for me:
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?Try
This will launch Internet Explorer and the URL. Process.Start does not detect applications/browsers automaticall.y
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.