问题描述
我有一个网址,我想启动它在默认浏览器。我试过两种方法:
I have a URL and I want to launch it in the default browser. I've tried two methods:
Process.Start("http://stackoverflow.com");
...和一个在此详述的other问题使用的ShellExecute。
... and the one detailed in this other question using ShellExecute.
在这两种情况下我得到的错误:Windows无法找到'http://stackoverflow.com。请确保您正确,键入名称,然后再试。
In both cases I get the error: Windows cannot find 'http://stackoverflow.com'. Make sure you typed the name correctly, and then try again.
这不应该试图打开它,就好像一个文件......从我个人理解,应该认识到它作为一个URL,并在默认浏览器中打开它。我缺少什么?
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?
顺便说一句:OS = Vista和.NET = 3.5
By the way: OS = Vista, and .NET = 3.5
修改
根据,因为的Process.Start设置默认的UseShellExecute,就应该启动默认浏览器
According to this MS KB article, since Process.Start sets the UseShellExecute by default, it should launch the default browser.
修改
下面就是不工作:
System.Diagnostics.Process.Start(@"C:\Program Files\Internet Explorer\IExplore.exe", "http://stackoverflow.com");
不幸的是,真正不启动的默认浏览器,它也如果未安装在正常处的IE不工作。我不知道这里做什么。
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.
更多信息
OK,所以我得到的错误是错误号-2147467259。看着谷歌对于这一点,看来,它不是很描述。这可能是一个文件关联错误或东西。
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.
的情节变得
所以我检查的应该有我的HTTP文件关联注册表项:
So I checked the registry key that's supposed to have my file association for http:
KEY_CLASSES_ROOT\http\shell\open\command\default
下面的值:
"C:\Program Files\Mozilla Firefox\firefox.exe" -requestPending -osint -url "%1"
这是有道理的。其实我这个复制串入一个命令提示符并取代%1和它的工作,打开Firefox浏览器。我只是不明白为什么的Process.Start不是URL用这个命令关联...
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...
推荐答案
好了,所以它神秘开始不做改变正常工作。我无法解释它。然而,在此同时,我写找到和执行的默认浏览器的另一种方法。这是一个有点哈克,但不仅仅只是默认IE加载好得多:
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();
}
这篇关于如何打开从.NET程序的Web浏览器?的Process.Start()不工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!