C# 调用浏览器打开网址

发布时间 2023-07-28 11:58:55作者: 无昵称老炮儿
  • 调用方法体
            public void OpenBrowser()
            {
                string url = "http://localhost:8055/api";
                bool openRes = OpenBrowserHelper.OpenChromeBrowserUrl(url);
    
                if (!openRes)
                    openRes = OpenBrowserHelper.OpenDefaultBrowserUrl(url);
    
                if (!openRes)
                {
                    // 打开下载链接,从官网下载 谷歌
                    if (MessageBox.Show("系统未安装谷歌(Chrome)浏览器,是否下载安装?", null, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        OpenBrowserHelper.OpenIe("https://www.google.cn/chrome/");
                    }
                }
            }
  • 打开浏览器帮助类
    public class OpenBrowserHelper
        {
            /// <summary>
            /// 调用谷歌(Chrome)浏览器
            /// </summary>
            /// <param name="url">打开网页的链接</param>
            public static bool OpenChromeBrowserUrl(string url)
            {
                bool isOpen = true;
                try
                {
                    // 64位注册表路径
                    var openKey = @"SOFTWARE\Wow6432Node\Google\Chrome";
                    if (IntPtr.Size == 4)
                    {
                        // 32位注册表路径
                        openKey = @"SOFTWARE\Google\Chrome";
                    }
                    RegistryKey appPath = Registry.LocalMachine.OpenSubKey(openKey);
                    // 谷歌浏览器就用谷歌打开,没找到就用系统默认的浏览器
                    // 谷歌卸载了,注册表还没有清空,程序会返回一个"系统找不到指定的文件。"的bug
                    if (appPath != null)
                    {
                        var result = Process.Start("chrome.exe", url);
                        if (result == null)
                        {
                            OpenDefaultBrowserUrl(url);
                        }
                    }
                    else
                    {
                        OpenDefaultBrowserUrl(url);
                    }
                }
                catch
                {
                    isOpen= false;
                }
    
                return isOpen;
            }
    
            /// <summary>
            /// 调用IE浏览器
            /// </summary>
            /// <param name="url"></param>
            public static void OpenIe(string url)
            {
                // IE浏览器路径安装:C:\Program Files\Internet Explorer
                // at System.Diagnostics.process.StartWithshellExecuteEx(ProcessStartInfo startInfo)注意这个错误
                try
                {
                    if (File.Exists(@"C:\Program Files\Internet Explorer\iexplore.exe"))
                    {
                        ProcessStartInfo processStartInfo = new ProcessStartInfo
                        {
                            FileName = @"C:\Program Files\Internet Explorer\iexplore.exe",
                            Arguments = url,
                            UseShellExecute = false,
                            CreateNoWindow = true
                        };
                        Process.Start(processStartInfo);
                    }
                    else
                    {
                        if (File.Exists(@"C:\Program Files (x86)\Internet Explorer\iexplore.exe"))
                        {
                            ProcessStartInfo processStartInfo = new ProcessStartInfo
                            {
                                FileName = @"C:\Program Files (x86)\Internet Explorer\iexplore.exe",
                                Arguments = url,
                                UseShellExecute = false,
                                CreateNoWindow = true
                            };
                            Process.Start(processStartInfo);
                        }                    
                    }
                }
                catch (Exception exception)
                {                
                }
            }
    
            /// <summary>
            /// 调用系统默认浏览器(用户自己设置了默认浏览器)
            /// </summary>
            /// <param name="url"></param>
            public static bool OpenDefaultBrowserUrl(string url)
            {
                bool isOpen = true;
                try
                {
                    //从注册表中读取默认浏览器可执行文件路径
                    RegistryKey key = Registry.ClassesRoot.OpenSubKey(@"http\shell\open\command\");
                    if (key != null)
                    {
                        string browserPath = string.Empty;
                        string[] splitArr = new string[] { };
                        string browser = key.GetValue("").ToString();
    
                        //browser是默认浏览器,不同的浏览器后面的参数不一样。例如:"D:\Program Files (x86)\Google\chrome.exe" -- "%1"
                        var lastIndex = browser.IndexOf(".exe", StringComparison.Ordinal);
                        if (lastIndex == -1)
                            lastIndex = browser.IndexOf(".EXE", StringComparison.Ordinal);
    
                        if (lastIndex != -1)
                        {
                            splitArr = browser.Split("\"");
                        }
                        //大于0 说明 按照 " 切割到数据
                        if (splitArr.Length > 0)
                        {
                            browserPath = splitArr[1];
                        }
                        else if (splitArr.Length == 0 && lastIndex != -1) //说明有浏览器,列如:D:\QQBrowser\QQBrowser.exe
                        {
                            browserPath = browser;
                        }
                        //打开浏览器
                        var result = Process.Start(browserPath, url);
    
                        //调用IE
                        //if (result == null)
                        //    OpenIe(url);
                    }
                    else
                    {
                        isOpen = false; 
                       //OpenIe(url);
                    }
                        
                }
                catch
                {
                    isOpen = false;
                }
                return isOpen;
            }
        }