C# 调用浏览器打开网址

农民  金牌会员 | 2023-7-28 12:04:24 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 881|帖子 881|积分 2643


  • 调用方法体
    1.         public void OpenBrowser()
    2.         {
    3.             string url = "http://localhost:8055/api";
    4.             bool openRes = OpenBrowserHelper.OpenChromeBrowserUrl(url);
    5.             if (!openRes)
    6.                 openRes = OpenBrowserHelper.OpenDefaultBrowserUrl(url);
    7.             if (!openRes)
    8.             {
    9.                 // 打开下载链接,从官网下载 谷歌
    10.                 if (MessageBox.Show("系统未安装谷歌(Chrome)浏览器,是否下载安装?", null, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question) == DialogResult.Yes)
    11.                 {
    12.                     OpenBrowserHelper.OpenIe("https://www.google.cn/chrome/");
    13.                 }
    14.             }
    15.         }
    复制代码
  • 打开浏览器帮助类
    1. public class OpenBrowserHelper
    2.     {
    3.         /// <summary>
    4.         /// 调用谷歌(Chrome)浏览器
    5.         /// </summary>
    6.         /// <param name="url">打开网页的链接</param>
    7.         public static bool OpenChromeBrowserUrl(string url)
    8.         {
    9.             bool isOpen = true;
    10.             try
    11.             {
    12.                 // 谷歌浏览器就用谷歌打开,没找到就用系统默认的浏览器
    13.                 // 谷歌卸载了,注册表还没有清空,程序会返回一个"系统找不到指定的文件。"的bug
    14.                 var chromeKey = @"\Software\Microsoft\Windows\CurrentVersion\App Paths\chrome.exe";
    15.                 // 通过注册表找到谷歌浏览器安装路径
    16.                 string chromeAppFileName = (string)(Registry.GetValue("HKEY_LOCAL_MACHINE" + chromeKey, "", null) ?? Registry.GetValue("HKEY_CURRENT_USER" + chromeKey, "", null));
    17.                 // 找到谷歌浏览器则打开
    18.                 if (!string.IsNullOrWhiteSpace(chromeAppFileName))
    19.                 {
    20.                     Process.Start(chromeAppFileName, url);
    21.                 }
    22.                 else
    23.                 {
    24.                     isOpen = false;
    25.                     //默认浏览器打开
    26.                    //OpenDefaultBrowserUrl(url);
    27.                 }
    28.             }
    29.             catch
    30.             {
    31.                 isOpen = false;
    32.             }
    33.             return isOpen;
    34.         }
    35.         /// <summary>
    36.         /// 调用IE浏览器
    37.         /// </summary>
    38.         /// <param name="url"></param>
    39.         public static void OpenIe(string url)
    40.         {
    41.             // IE浏览器路径安装:C:\Program Files\Internet Explorer
    42.             // at System.Diagnostics.process.StartWithshellExecuteEx(ProcessStartInfo startInfo)注意这个错误
    43.             try
    44.             {
    45.                 if (File.Exists(@"C:\Program Files\Internet Explorer\iexplore.exe"))
    46.                 {
    47.                     ProcessStartInfo processStartInfo = new ProcessStartInfo
    48.                     {
    49.                         FileName = @"C:\Program Files\Internet Explorer\iexplore.exe",
    50.                         Arguments = url,
    51.                         UseShellExecute = false,
    52.                         CreateNoWindow = true
    53.                     };
    54.                     Process.Start(processStartInfo);
    55.                 }
    56.                 else
    57.                 {
    58.                     if (File.Exists(@"C:\Program Files (x86)\Internet Explorer\iexplore.exe"))
    59.                     {
    60.                         ProcessStartInfo processStartInfo = new ProcessStartInfo
    61.                         {
    62.                             FileName = @"C:\Program Files (x86)\Internet Explorer\iexplore.exe",
    63.                             Arguments = url,
    64.                             UseShellExecute = false,
    65.                             CreateNoWindow = true
    66.                         };
    67.                         Process.Start(processStartInfo);
    68.                     }                    
    69.                 }
    70.             }
    71.             catch (Exception exception)
    72.             {               
    73.             }
    74.         }
    75.         /// <summary>
    76.         /// 调用系统默认浏览器(用户自己设置了默认浏览器)
    77.         /// </summary>
    78.         /// <param name="url"></param>
    79.         public static bool OpenDefaultBrowserUrl(string url)
    80.         {
    81.             bool isOpen = true;
    82.             try
    83.             {
    84.                 //从注册表中读取默认浏览器可执行文件路径
    85.                 RegistryKey key = Registry.ClassesRoot.OpenSubKey(@"http\shell\open\command");
    86.                 if (key != null)
    87.                 {
    88.                     string browserPath = string.Empty;
    89.                     string[] splitArr = new string[] { };
    90.                     string browser = key.GetValue("").ToString();
    91.                     //browser是默认浏览器,不同的浏览器后面的参数不一样。例如:"D:\Program Files (x86)\Google\chrome.exe" -- "%1"
    92.                     var lastIndex = browser.IndexOf(".exe", StringComparison.Ordinal);
    93.                     if (lastIndex == -1)
    94.                         lastIndex = browser.IndexOf(".EXE", StringComparison.Ordinal);
    95.                     if (lastIndex != -1)
    96.                     {
    97.                         splitArr = browser.Split(""");
    98.                     }
    99.                     //大于0 说明 按照 " 切割到数据
    100.                     if (splitArr.Length > 0)
    101.                     {
    102.                         browserPath = splitArr[1];
    103.                     }
    104.                     else if (splitArr.Length == 0 && lastIndex != -1) //说明有浏览器,列如:D:\QQBrowser\QQBrowser.exe
    105.                     {
    106.                         browserPath = browser;
    107.                     }
    108.                     //打开浏览器
    109.                     var result = Process.Start(browserPath, url);
    110.                     //调用IE
    111.                     //if (result == null)
    112.                     //    OpenIe(url);
    113.                 }
    114.                 else
    115.                 {
    116.                     isOpen = false;
    117.                    //OpenIe(url);
    118.                 }
    119.                     
    120.             }
    121.             catch
    122.             {
    123.                 isOpen = false;
    124.             }
    125.             return isOpen;
    126.         }
    127.     }
    复制代码
     

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

农民

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表