如何在非默认的 Web 浏览器中打开本地 HTML 文件? - C#

3
我想在非默认的浏览器中打开本地html文件。目前为止,我可以使用以下代码在默认浏览器中打开html文件: System.Diagnostics.Process.Start("文件位置"); 但是有没有办法在非默认浏览器中打开此文件呢?
如果我能通过进程获取webBrowser对象就太好了。我已经找到了如何确定所需的浏览器是否已打开: var runningProcess = System.Diagnostics.Process.GetProcessesByName("chrome"); if (runningProcess.Length != 0) { } 同时我不能更改默认浏览器。
谢谢。
3个回答

3
您可以使用以下代码:
在线页面:
System.Diagnostics.Process.Start("C:\Program Files (x86)\Google\Chrome\Application\chrome.exe", "http://wwww.testdomain.com/mypage.html");

离线页面:

System.Diagnostics.Process.Start("C:\Program Files (x86)\Google\Chrome\Application\chrome.exe", "C:\Users\AppData\Local\Temp\mypage.html");

2
如果您想从c#中在Chrome中打开网站,请尝试以下操作:
System.Diagnostics.Process.Start("chrome", "www.google.com");

使用您的代码,我认为您是首先尝试获取任何打开的浏览器?
var runningProcess = System.Diagnostics.Process.GetProcessesByName("chrome");
if (runningProcess.Length != 0)
{
    System.Diagnostics.Process.Start("chrome", filename);
}
runningProcess = System.Diagnostics.Process.GetProcessesByName("firefox");
if (runningProcess.Length != 0)
{
    System.Diagnostics.Process.Start("firefox", filename);
}
runningProcess = System.Diagnostics.Process.GetProcessesByName("iexplore");
if (runningProcess.Length != 0)
{
    System.Diagnostics.Process.Start("iexplore", filename);
}

谢谢,这正是我要找的。 - Marc-Antoine Charland

0

MS链接已损坏。 - Michael Freidgeim

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