公司的winform项目中需要在点击菜单时用谷歌浏览器打开网页,找了很久看到一个博主发的,亲测可用,记录一下备用。

原文:https://www.cnblogs.com/wohexiaocai/p/4522046.html

     public static void OpenBrowserUrl(string url)
        {
            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)
                    {
                        OpenIe(url);
                    }
                }
                else
                {
                    var result = Process.Start("chrome.exe", url);
                    if (result == null)
                    {
                        OpenIe(url);
                    }
                }
            }
            catch
            {
                //出错调用IE
                OpenIe(url);
            }
        }
        /// <summary>
        /// 用IE打开浏览器
        /// </summary>
        /// <param name="url"></param>
        public static void OpenIe(string url)
        {
            System.Diagnostics.Process process = new System.Diagnostics.Process();
            process.StartInfo.FileName = "iexplore.exe";   //IE浏览器,可以更换
            process.StartInfo.Arguments = url;
            process.Start();
        }