admin 管理员组文章数量: 887018
C#打开系统浏览器(默认浏览器,谷歌,火狐等...可以自行扩展)
- Talk is cheap,Show code
Talk is cheap,Show code
using Microsoft.Win32;
using System;
using System.Diagnostics;
using System.IO;
namespace LingbugUtils.Services
{
public class BrowserService
{
public static void OpenGoogleBrowserUrl(string url)
{
try
{
//注册表路径
var openKey = IsWin32() ? @"SOFTWARE\Google\Chrome" : @"SOFTWARE\Wow6432Node\Google\Chrome";
//打开注册表
var appPath = Registry.LocalMachine.OpenSubKey(openKey);
if (appPath != null)
{
//用谷歌打开(如果谷歌卸载了,但是注册表没有清空,会有bug)
var result = Process.Start("chrome.exe", url);
//用IE打开
if (result == null) OpenIeBrowserUrl(url);
}
else
{
//用谷歌打开
var result = Process.Start("chrome.exe", url);
//用默认浏览器打开
if (result == null) OpenDefaultBrowserUrl(url);
}
}
catch (Exception ex)
{
//记录日志
LogService.Info($"{nameof(OpenGoogleBrowserUrl)} - Error【{ex.Message}】:{ex.ToJson()}");
//调用默认浏览器打开
OpenDefaultBrowserUrl(url);
}
}
public static void OpenIeBrowserUrl(string url)
{
try
{
//打开IE
Process.Start("iexplore.exe", url);
}
catch (Exception ex)
{
//记录日志
LogService.Info($"{nameof(OpenIeBrowserUrl)} - Error【{ex.Message}】:{ex.ToJson()}");
try
{
//ie浏览器路径
string iePath = @"C:\Program Files\Internet Explorer\iexplore.exe";
if (File.Exists(iePath))
{
//参数
var processStartInfo = new ProcessStartInfo
{
FileName = iePath,
Arguments = url,
UseShellExecute = false,
CreateNoWindow = true
};
//打开
Process.Start(processStartInfo);
}
else
{
//ie浏览器路径
iePath = @"C:\Program Files (x86)\Internet Explorer\iexplore.exe";
if (File.Exists(iePath))
{
//参数
var processStartInfo = new ProcessStartInfo
{
FileName = iePath,
Arguments = url,
UseShellExecute = false,
CreateNoWindow = true
};
//打开
Process.Start(processStartInfo);
}
else
{
//提示
throw new Exception("系统未安装IE浏览器");
//if (MessageBox.Show(@"系统未安装IE浏览器,是否下载安装?", null, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question) == DialogResult.Yes)
//{
// //打开下载IE网站
// OpenDefaultBrowserUrl("http://windows.microsoft/zh-cn/internet-explorer/download-ie");
//}
}
}
}
catch (Exception exception)
{
//记录日志
LogService.Info($"{nameof(OpenIeBrowserUrl)} - Again - Error【{exception.Message}】:{exception.ToJson()}");
}
}
}
public static void OpenDefaultBrowserUrl(string url)
{
try
{
//读取注册表
var key = Registry.ClassesRoot.OpenSubKey(@"http\shell\open\command\");
if (key != null)
{
//读取(s就是默认浏览器路径,参数格式:"D:\Program Files (x86)\Google\Chrome\Application\chrome.exe" -- "%1")
string s = key.GetValue("").ToString();
//索引
var lastIndex = s.IndexOf(".exe", StringComparison.Ordinal);
//索引
if (lastIndex == -1) lastIndex = s.IndexOf(".EXE", StringComparison.Ordinal);
//路径
var path = s.Substring(1, lastIndex + 3);
//打开
var result = Process.Start(path, url);
if (result == null)
{
//打开
var result1 = Process.Start("explorer.exe", url);
//打开
if (result1 == null) Process.Start(url);
}
}
else
{
//打开
var result1 = Process.Start("explorer.exe", url);
//打开
if (result1 == null) Process.Start(url);
}
}
catch (Exception ex)
{
//记录日志
LogService.Info($"{nameof(OpenDefaultBrowserUrl)} - Error【{ex.Message}】:{ex.ToJson()}");
//用IE打开
OpenIeBrowserUrl(url);
}
}
public static void OpenFireFoxBrowserUrl(string url)
{
try
{
//注册表路径
var openKey = IsWin32()
? @"SOFTWARE\Mozilla\Mozilla Firefox"
: @"SOFTWARE\Wow6432Node\Mozilla\Mozilla Firefox";
//读取
var appPath = Registry.LocalMachine.OpenSubKey(openKey);
if (appPath != null)
{
//打开火狐
var result = Process.Start("firefox.exe", url);
//打开IE
if (result == null) OpenIeBrowserUrl(url);
}
else
{
//打开火狐
var result = Process.Start("firefox.exe", url);
//打开默认浏览器
if (result == null) OpenDefaultBrowserUrl(url);
}
}
catch (Exception ex)
{
//记录日志
LogService.Info($"{nameof(OpenFireFoxBrowserUrl)} - Error【{ex.Message}】:{ex.ToJson()}");
//打开默认浏览器
OpenDefaultBrowserUrl(url);
}
}
//是否是32位
private static bool IsWin32() => IntPtr.Size == 4;
}
}
版权声明:本文标题:C#打开系统浏览器(默认浏览器,谷歌,火狐等...可以自行扩展) 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.freenas.com.cn/jishu/1726853888h1039970.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论