【问题标题】:Open a html file using default web browser使用默认网络浏览器打开一个 html 文件
【发布时间】:2012-06-12 02:03:42
【问题描述】:

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

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(我电脑上安装的两个网络浏览器),而不是默认的网络浏览器 Internet Explorer。我不知道如何解决这个问题。

编辑

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

【问题讨论】:

    标签: c# browser


    【解决方案1】:

    您可以将所有代码替换为

    System.Diagnostics.Process.Start(pathToHtmlFile);
    

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

    现在将 Firefox 设置为默认设置,这有时会导致奇怪的异常(我认为如果 Firefox 是第一次启动),因此您可能需要对其进行try/catch 处理。

    【讨论】:

    • 我试过了。但在某些个人电脑中,.htm/.html 不是用网络浏览器打开的。例如,.htm/.html 扩展名可以与文本编辑器或 IDE 相关联。
    • 虽然默认程序可以改变,但你应该没有问题。有关使用 ShellExecute 启动默认 Web 浏览器的一些提示,以及一些注册表项的路径(您可能已经知道),请参阅 this。最后,用户很可能可以通过更改一些默认程序来进行干预......但你不应该太担心这一点,因为它在一定程度上是不可避免的。
    • 正如杰克所说,这是一个糟糕的主意。我个人有一个编辑器设置为默认应用程序来打开 HTML 文件,看到程序在该编辑器而不是默认浏览器中打开它们的自述文件让我感到不安。 Windows 中的“默认浏览器”和“用于打开 HTML 的默认应用程序”之间存在差异。
    • 这不应该是公认的答案。不保证文件会用浏览器打开。
    • @Theyouthis 如果您有更好的解决方案,请随时提供帮助
    【解决方案2】:

    .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: The specified executable is not an valid application for this OS platform.

    【讨论】:

    • This 实际上是首选方式。
    【解决方案3】:

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

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

    【讨论】:

    • 假设用户安装了chrome浏览器。
    • DefaultWebBrowser 的代码在页面顶部给出
    • 如果您希望它更通用,请返回使用该代码(对不起,假设它很明显)。
    • 好的,我看到你原来的问题是 DefaulWebBrowser 的问题。 - "上面的函数调用firefox和IE".
    • 没有用,即使我安装了 chrome(默认)
    【解决方案4】:

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

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

    【讨论】:

      【解决方案5】:

      我在使用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);
      }
      

      【讨论】:

        【解决方案6】:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-10-03
          • 1970-01-01
          • 1970-01-01
          • 2011-03-11
          • 2012-04-17
          • 2014-12-21
          • 2014-12-06
          • 1970-01-01
          相关资源
          最近更新 更多