如何从 .NET 程序打开 Web 浏览器? Process.Start() 不起作用?
我有一个 URL,我想在默认浏览器中启动它。我尝试了两种方法:
Process.Start("http://stackoverflow.com");
...以及 使用 ShellExecute 的其他问题。
在这两种情况下,我都会收到错误:Windows 找不到“http://stackoverflow.com”。确保您输入的名称正确,然后重试。
但它不应该尝试将其作为文件打开...据我了解,它应该将其识别为 URL 并在默认浏览器中打开它。我缺少什么?
顺便说一句:操作系统= Vista,.NET = 3.5
编辑:
根据this MS 知识库文章,由于 Process.Start 默认设置 UseShellExecute,因此它应该启动默认浏览器。
编辑:
下面是有效的方法:
System.Diagnostics.Process.Start(@"C:\Program Files\Internet Explorer\IExplore.exe", "http://stackoverflow.com");
不幸的是,这确实不会启动默认浏览器,而且如果 IE 未安装在“正常”位置,它也不起作用。我不知道在这里做什么。
更多信息:
好的,所以我收到的错误是错误号-2147467259。谷歌搜索了一下,似乎描述性不太好。可能是文件关联错误之类的。
情节变得更加复杂:
所以我检查了应该与http文件关联的注册表项:
KEY_CLASSES_ROOT\http\shell\open\command\default
这是值:
"C:\Program Files\Mozilla Firefox\firefox.exe" -requestPending -osint -url "%1"
这是有道理的。实际上,我将此字符串复制到命令提示符中,并将 %1 替换为 http://stackoverflow.com,它工作并打开了 Firefox。我只是不明白为什么 Process.Start 没有将 URL 与此命令相关联......
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...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试
此操作将启动 Internet Explorer 和 URL。 Process.Start 不会自动检测应用程序/浏览器。y
Try
This will launch Internet Explorer and the URL. Process.Start does not detect applications/browsers automaticall.y
当 Firefox 是默认 Web 浏览器时,我发现这是一个严重的问题。
如果我们使用 System.Windows.Forms.Help.ShowHelp(null, "http://microsoft.com"),此类错误消息可以在 Windows 上解决。但是,Help.ShowHelp 在 Mono/openSUSE 上无法按预期工作。
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.
好吧,它神秘地开始正常工作,没有做任何改变。我无法解释。然而,与此同时,我编写了另一种查找并执行默认浏览器的方法。这有点 hacky,但比默认加载 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:
这对我有用:
不要忘记 UseShellExecute 如果您想使用命令类型的自动识别(在本例中为http/浏览器)。
编辑:如果您
Win+R
网址,它会起作用吗?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?