使用默认的Web浏览器打开html文件 [英] Open a html file using default web browser
问题描述
我正在使用它来获取默认Web浏览器的路径和可执行文件:
公共静态字符串DefaultWebBrowser
{
get
{
字符串路径= @ \http\shell\open\command;
使用(RegistryKey reg = Registry.ClassesRoot.OpenSubKey(path))
{
if(reg!= null)
{
string webBrowserPath = reg.GetValue(String.Empty)作为字符串;
if(!String.IsNullOrEmpty(webBrowserPath))
{
if(webBrowserPath.First()=='')
{
返回webBrowserPath.Split('')[1];
}
return webBrowserPath.Split(’)[0];
}
}
返回null;
}
}
}
并且:
受保护的静态布尔值Run(string FileName,string Args)
{
try
{
流程proc = new Process();
processInfo.FileName = FileName;
proc.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
if(Args!= null)proc.StartInfo.Arguments = Args;
proc.Start();
返回true;
}
catch(异常){}
返回false;
}
然后我调用Web浏览器: Run(DefaultWebBrowser , foo.html)
问题是:上面的函数正在调用Firefox和IE(我的电脑上安装了两个Web浏览器),而不是Internet Explorer(默认的网络浏览器)。我不知道如何解决这个问题。
EDIT
我已经下载了并安装了Google Chrome浏览器,将其设置为默认的网络浏览器,但奇怪的是上面的错误不会发生。
您可以将所有代码替换为
System.Diagnostics.Process.Start(pathToHtmlFile);
这将自动启动您的默认浏览器,或者查找 .htm
或 .html
文件并使用该文件。
现在已设置Firefox默认情况下,这有时会导致奇怪的异常(我想如果Firefox是第一次启动),所以您可能要对其进行 try / catch
来处理。 / p>
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")
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.
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.
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 .htm
or .html
files and use that.
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/catch
on it to handle that.
这篇关于使用默认的Web浏览器打开html文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!