我正在使用它来获取默认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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
和:
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 Code Online (Sandbox Code Playgroud)
然后我调用Web浏览器: Run(DefaultWebBrowser, "foo.html")
问题是:上面的功能是调用Firefox和IE(我的电脑上安装的两个Web浏览器)而不是Internet Explorer,即默认的Web浏览器.我不知道如何解决这个问题.
编辑
我已经下载并安装了谷歌浏览器,将其设置为默认的网络浏览器,但奇怪的是上述错误不会发生.
Dar*_*o Z 31
您可以用所有代码替换
System.Diagnostics.Process.Start(pathToHtmlFile);
Run Code Online (Sandbox Code Playgroud)
这将自动启动您的默认浏览器,或者更确切地查找.htm
或.html
文件的默认处理程序并使用它.
现在将Firefox设置为默认值,这有时会导致奇怪的异常(我想如果Firefox第一次启动),所以你可能想要try/catch
对它进行处理.
对于.Net Core,您需要调用(建议在.Net Core 2.0 Process.Start中引发“指定的可执行文件不是此OS平台的有效应用程序”)
var proc = Process.Start(@"cmd.exe ", @"/c " + pathToHtmlFile);
Run Code Online (Sandbox Code Playgroud)
尝试时Process.Start(pathToHtmlFile);
,出现System.ComponentModel.Win32Exception:指定的可执行文件不是此OS平台的有效应用程序。
小智 6
如果您遇到System.ComponentModel.Win32Exception
异常,您需要设置UseShellExecute
为true
var p = new Process();
p.StartInfo = new ProcessStartInfo(@"C:\path\test.html")
{
UseShellExecute = true
};
p.Start();
Run Code Online (Sandbox Code Playgroud)
请参阅.Net Core 2.0 Process.Start 抛出“指定的可执行文件不是此操作系统平台的有效应用程序”
归档时间: |
|
查看次数: |
35608 次 |
最近记录: |