-
调用方法体
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 { // 谷歌浏览器就用谷歌打开,没找到就用系统默认的浏览器 // 谷歌卸载了,注册表还没有清空,程序会返回一个"系统找不到指定的文件。"的bug var chromeKey = @"SoftwareMicrosoftWindowsCurrentVersionApp Pathschrome.exe"; // 通过注册表找到谷歌浏览器安装路径 string chromeAppFileName = (string)(Registry.GetValue("HKEY_LOCAL_MACHINE" + chromeKey, "", null) ?? Registry.GetValue("HKEY_CURRENT_USER" + chromeKey, "", null)); // 找到谷歌浏览器则打开 if (!string.IsNullOrWhiteSpace(chromeAppFileName)) { Process.Start(chromeAppFileName, url); } else { isOpen = false; //默认浏览器打开 //OpenDefaultBrowserUrl(url); } } catch { isOpen = false; } return isOpen; } /// <summary> /// 调用IE浏览器 /// </summary> /// <param name="url"></param> public static void OpenIe(string url) { // IE浏览器路径安装:C:Program FilesInternet Explorer // at System.Diagnostics.process.StartWithshellExecuteEx(ProcessStartInfo startInfo)注意这个错误 try { if (File.Exists(@"C:Program FilesInternet Exploreriexplore.exe")) { ProcessStartInfo processStartInfo = new ProcessStartInfo { FileName = @"C:Program FilesInternet Exploreriexplore.exe", Arguments = url, UseShellExecute = false, CreateNoWindow = true }; Process.Start(processStartInfo); } else { if (File.Exists(@"C:Program Files (x86)Internet Exploreriexplore.exe")) { ProcessStartInfo processStartInfo = new ProcessStartInfo { FileName = @"C:Program Files (x86)Internet Exploreriexplore.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(@"httpshellopencommand"); if (key != null) { string browserPath = string.Empty; string[] splitArr = new string[] { }; string browser = key.GetValue("").ToString(); //browser是默认浏览器,不同的浏览器后面的参数不一样。例如:"D:Program Files (x86)Googlechrome.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:QQBrowserQQBrowser.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; } }
-
C# 调用浏览器打开网址
出处:https://www.cnblogs.com/LaoPaoEr/p/17587220.html
栏目列表
最新更新
求1000阶乘的结果末尾有多少个0
详解MyBatis延迟加载是如何实现的
IDEA 控制台中文乱码4种解决方案
SpringBoot中版本兼容性处理的实现示例
Spring的IOC解决程序耦合的实现
详解Spring多数据源如何切换
Java报错:UnsupportedOperationException in Col
使用Spring Batch实现批处理任务的详细教程
java中怎么将多个音频文件拼接合成一个
SpringBoot整合ES多个精确值查询 terms功能实
数据库审计与智能监控:从日志分析到异
SQL Server 中的数据类型隐式转换问题
SQL Server中T-SQL 数据类型转换详解
sqlserver 数据类型转换小实验
SQL Server数据类型转换方法
SQL Server 2017无法连接到服务器的问题解决
SQLServer地址搜索性能优化
Sql Server查询性能优化之不可小觑的书签查
SQL Server数据库的高性能优化经验总结
SQL SERVER性能优化综述(很好的总结,不要错
uniapp/H5 获取手机桌面壁纸 (静态壁纸)
[前端] DNS解析与优化
为什么在js中需要添加addEventListener()?
JS模块化系统
js通过Object.defineProperty() 定义和控制对象
这是目前我见过最好的跨域解决方案!
减少回流与重绘
减少回流与重绘
如何使用KrpanoToolJS在浏览器切图
performance.now() 与 Date.now() 对比