Net Core教程

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

本文主要是介绍C#打开系统浏览器(默认浏览器,谷歌,火狐等...可以自行扩展),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

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


这篇关于C#打开系统浏览器(默认浏览器,谷歌,火狐等...可以自行扩展)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!