C#打开系统浏览器(默认浏览器,谷歌,火狐等...可以自行扩展)

C#打开系统浏览器(默认浏览器,谷歌,火狐等...可以自行扩展)

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.com/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;
    }
}


上一篇:概率论与数理统计(4):随机变量的数字特征


下一篇:C#中给WebClient添加代理Proxy