C# 使用默认 Web 浏览器打开 html 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10989709/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Open a html file using default web browser
提问by Hyman
I'm using this to get the path and executable of default web browser:
我正在使用它来获取默认网络浏览器的路径和可执行文件:
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;
}
}
}
And:
和:
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;
}
Then I call the web browser: Run(DefaultWebBrowser, "foo.html")
然后我调用网络浏览器: Run(DefaultWebBrowser, "foo.html")
The question is: the above function is calling Firefox and IE (the two web browsers installed on my pc) instead of Internet Explorer, the default web browser. I have no idea how to fix this.
问题是:上述函数调用的是 Firefox 和 IE(我电脑上安装的两个 Web 浏览器),而不是默认的 Web 浏览器 Internet Explorer。我不知道如何解决这个问题。
EDIT
编辑
I have downloaded and installed the Google Chrome, set it as default web browser, but oddly the above error don't happens with it.
我已经下载并安装了谷歌浏览器,将其设置为默认的网络浏览器,但奇怪的是,上面的错误并没有发生。
采纳答案by Darko Z
You can replace all that code with
你可以用
System.Diagnostics.Process.Start(pathToHtmlFile);
This will automatically start your default browser, or rather look up the default handler for .htmor .htmlfiles and use that.
这将自动启动您的默认浏览器,或者查找.htm或.html文件的默认处理程序并使用它。
Now with Firefox set as default this can sometimes cause weird exceptions (I think if Firefox is starting for first time), so you might want to do a try/catchon it to handle that.
现在将 Firefox 设置为默认值,这有时会导致奇怪的异常(我认为如果 Firefox 是第一次启动),因此您可能想要try/catch对其进行处理来处理它。
回答by Richard Matsen
For those who don't have html default association to a browser, use
对于那些没有与浏览器的 html 默认关联的人,请使用
System.Diagnostics.Process.Start("Chrome", Uri.EscapeDataString(pathToHtmlFile))
System.Diagnostics.Process.Start("Chrome", Uri.EscapeDataString(pathToHtmlFile))
回答by Dávid Mi?kovi?
Im using a code, where i look up for exe files first. For example, if exsists chrome.exe (in its default path) else if exists firefox.exe or launcher.exe (for opera) etc... if doesnt any exists, try to run iexplore.exe with pathToHtmlFile parameter. This is my solution, where i use external config, where a set my browser, doesnt matter what is set default in OS.
我使用代码,我首先在其中查找 exe 文件。例如,如果存在 chrome.exe(在其默认路径中),否则如果存在 firefox.exe 或 launcher.exe(用于歌剧)等...如果不存在,请尝试使用 pathToHtmlFile 参数运行 iexplore.exe。这是我的解决方案,我使用外部配置,其中设置我的浏览器,与操作系统中的默认设置无关。
回答by Michael Freidgeim
For .Net Core you need to call (suggested in .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);
When I tried Process.Start(pathToHtmlFile);,
I've got System.ComponentModel.Win32Exception: The specified executable is not a valid application for this OS platform.
当我尝试时Process.Start(pathToHtmlFile);,我得到System.ComponentModel.Win32Exception: The specified executable is not a valid application for this OS platform。

