使用默认 Web 浏览器打开 html 文件

本文关键字:html 文件 浏览器 默认 Web | 更新日期: 2023-09-27 18:37:02

我正在使用它来获取默认Web浏览器的路径和可执行文件:

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

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

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

编辑

我已经下载并安装了谷歌浏览器,将其设置为默认的网络浏览器,但奇怪的是,上述错误并没有发生。

使用默认 Web 浏览器打开 html 文件

您可以将所有代码替换为

System.Diagnostics.Process.Start(pathToHtmlFile);

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

现在将 Firefox

设置为默认值,这有时会导致奇怪的异常(我认为如果 Firefox 是第一次启动),所以你可能想对它做一个try/catch来处理这个问题。

2022 年 4 月 30 日更新:对于 .Net Core,更好的解决方案是按照 .Net Core 2.0 进程中的建议设置 UseShellExecute = true。

原建议:对于 .Net Core,您需要调用(在 .Net Core 2.0 进程中建议。Start 抛出"指定的可执行文件不是此操作系统平台的有效应用程序")

 var proc = Process.Start(@"cmd.exe ", @"/c " + pathToHtmlFile); 

当我尝试Process.Start(pathToHtmlFile);时,我有System.ComponentModel.Win32Exception:指定的可执行文件不是此操作系统平台的有效应用程序

如果您遇到异常System.ComponentModel.Win32Exception则需要将UseShellExecute设置为 true

var p = new Process();
p.StartInfo = new ProcessStartInfo(@"C:'path'test.html")
{
    UseShellExecute = true
};
p.Start();

请参阅 .Net Core 2.0 进程。Start 引发"指定的可执行文件不是此操作系统平台的有效应用程序"

对于那些没有 html 默认关联的浏览器的用户,请使用

System.Diagnostics.Process.Start("Chrome", Uri.EscapeDataString(pathToHtmlFile))

我在

使用System.Diagnostics.Process.Start(pathToHtmlFile);时收到此错误:

System.ComponentModel.Win32Exception: 'The specified executable is not a valid application for this OS platform. '

我能够使用以下代码在默认浏览器中打开 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);
}

我使用代码,我首先查找exe文件。例如,如果 exsist chrome.exe(在其默认路径中)否则如果存在火狐.exe或启动器.exe(用于歌剧)等......如果不存在,请尝试使用 pathToHtmlFile 参数运行 iexplore.exe。这是我的解决方案,我使用外部配置,设置我的浏览器,在操作系统中设置默认值并不重要。