如何从 .NET 程序打开 Web 浏览器? Process.Start() 不起作用?

发布于 2024-09-01 00:19:00 字数 1450 浏览 3 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

初吻给了烟 2024-09-08 00:19:29

尝试

Process.Start("IExplore.exe http://www.stackoverflow.com");

此操作将启动 Internet Explorer 和 URL。 Process.Start 不会自动检测应用程序/浏览器。y

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

囍孤女 2024-09-08 00:19:25

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

旧人 2024-09-08 00:19:22

好吧,它神秘地开始正常工作,没有做任何改变。我无法解释。然而,与此同时,我编写了另一种查找并执行默认浏览器的方法。这有点 hacky,但比默认加载 IE 好得多:

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();
}

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();
}
断爱 2024-09-08 00:19:18

这对我有用:

Process proc = new Process ();
proc.StartInfo.UseShellExecute = true;
proc.StartInfo.FileName = "http://stackoverflow.com";
proc.Start ();

不要忘记 UseShellExecute 如果您想使用命令类型的自动识别(在本例中为http/浏览器)。

编辑:如果您 Win+R 网址,它会起作用吗?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文