使用默认的网络浏览器打开HTML文件

21

我正在使用以下代码获取默认浏览器的路径和可执行文件:

public static string DefaultWebBrowser
        {
            get
            {

                string path = @"\http\shell\open\command";

                using (RegistryKey reg = Registry.ClassesRoot.OpenSubKey(path))
                {
                    if (reg != null)
                    {
                        string webBrowserPath = reg.GetValue(String.Empty) as string;

                        if (!String.IsNullOrEmpty(webBrowserPath))
                        {
                            if (webBrowserPath.First() == '"')
                            {
                                return webBrowserPath.Split('"')[1];
                            }

                            return webBrowserPath.Split(' ')[0];
                        }
                    }

                    return null;
                }
            }
        }

并且:

 protected static bool Run(string FileName, string Args)
        {
            try
            {
                Process proc = new Process();

                processInfo.FileName = FileName;
                 proc.StartInfo.WindowStyle = ProcessWindowStyle.Normal;

                if(Args != null) proc.StartInfo.Arguments = Args;

                proc.Start();

                return true;
            }
            catch (Exception) { }

            return false;
        }

然后我调用Web浏览器:Run(DefaultWebBrowser, "foo.html")

问题在于:上面的函数正在调用Firefox和IE (我电脑上安装的两个Web浏览器),而不是默认的Internet Explorer。我不知道如何解决这个问题。

编辑

我已经下载并安装了Google Chrome,并将其设置为默认Web浏览器,但奇怪的是,它不会发生上述错误。

6个回答

44

您可以使用以下代码替换所有的代码:

System.Diagnostics.Process.Start(pathToHtmlFile);

这将自动启动您的默认浏览器,或查找.htm.html文件的默认处理程序并使用它。

现在如果将Firefox设置为默认浏览器,有时会导致奇怪的异常(我认为是因为Firefox第一次启动),因此您可能需要使用try/catch来处理它。


3
我尝试过了,但是在某些电脑上,.htm/.html文件无法用浏览器打开。.htm/.html扩展名可能会与文本编辑器或IDE等其他程序相关联。 - Jack
1
虽然默认程序可以更改,但你不应该真的有问题。请参考此链接了解如何使用ShellExecute启动默认的Web浏览器以及一些注册表键的路径(你可能已经知道)。最终,用户很可能会通过更改一些默认程序来干预...但你不应该过于担心这个问题,因为在某种程度上是不可避免的。 - Spooky
1
正如Jack所说,这是一个糟糕的想法。我个人将编辑器设置为打开HTML文件的默认应用程序,看到程序在该编辑器中打开自己的readme文件而不是默认浏览器,这让我感到非常不安。在Windows中,“默认浏览器”和“默认打开HTML的应用程序”之间是有区别的。 - Nyerguds
1
这不应该是被接受的答案。不能保证文件会在浏览器中打开。 - BillHaggerty
@Theyouthis 如果你有更好的解决方案,请随时贡献。 - Darko

22

2022年4月30日更新:对于.Net Core,更好的解决方法是设置UseShellExecute = true,如.Net Core 2.0 Process.Start throws "The specified executable is not a valid application for this OS platform"中建议的那样。

原始建议: 对于.Net Core,您需要调用(建议在.Net Core 2.0 Process.Start throws "The specified executable is not a valid application for this OS platform"中提到)

 var proc = Process.Start(@"cmd.exe ", @"/c " + pathToHtmlFile); 
当我尝试使用Process.Start(pathToHtmlFile);时,我收到了System.ComponentModel.Win32Exception:指定的可执行文件不是此操作系统平台上的有效应用程序.

2
实际上,这种方式会更好。 - Marc.2377

6

1
对于那些没有将html默认关联到浏览器的人,请使用以下代码: System.Diagnostics.Process.Start("Chrome", Uri.EscapeDataString(pathToHtmlFile))
它会启动Chrome浏览器并打开指定的html文件。

它假设用户已经安装了Chrome浏览器。 - Jack
默认网络浏览器的代码在页面顶部给出。 - Richard Matsen
如果你想要更通用的代码,请回到使用那段代码(抱歉,之前假定这很明显)。 - Richard Matsen
好的,我明白你最初的问题是默认浏览器出了一些问题。上述函数正在调用Firefox和IE。 - Richard Matsen
由于DefaultWebBrowser仅返回单个字符串,很难看出发生了什么。也许与解析'reg.GetValue(String.Empty) as string'返回值有关。 - Richard Matsen
2
尽管我已经安装了Chrome(默认浏览器),但它仍然无法工作。 - z0mbi3

0
当我使用System.Diagnostics.Process.Start(pathToHtmlFile);时,出现了以下错误:

System.ComponentModel.Win32Exception:“指定的可执行文件不是此操作系统平台的有效应用程序。”

我能够使用以下代码在默认浏览器中打开html文件:

System.Diagnostics.Process process = new System.Diagnostics.Process();
try
{
    process.StartInfo.UseShellExecute = true;
    process.StartInfo.FileName = pathToHtmlFile;
    process.Start();
}
catch (Exception e)
{
    Console.WriteLine(e.Message);
}

-1

我正在使用一段代码,首先查找exe文件。 例如,如果chrome.exe存在(在其默认路径中),否则如果firefox.exe或launcher.exe(用于opera)等存在...如果不存在任何文件,则尝试使用pathToHtmlFile参数运行iexplore.exe。这是我的解决方案,我使用外部配置,在其中设置了我的浏览器,无论操作系统中设置的默认值是什么。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接