终端一体化运控平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

208 line
7.7 KiB

  1. using Microsoft.Win32;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace BPASmartClient.Model
  9. {
  10. public class BrowserHelper
  11. {
  12. /// <summary>
  13. /// 调用系统浏览器打开网页
  14. /// http://m.jb51.net/article/44622.htm
  15. /// http://www.2cto.com/kf/201412/365633.html
  16. /// </summary>
  17. /// <param name="url">打开网页的链接</param>
  18. public static void OpenBrowserUrl(string url)
  19. {
  20. try
  21. {
  22. // 64位注册表路径
  23. var openKey = @"SOFTWARE\Wow6432Node\Google\Chrome";
  24. if (IntPtr.Size == 4)
  25. {
  26. // 32位注册表路径
  27. openKey = @"SOFTWARE\Google\Chrome";
  28. }
  29. RegistryKey appPath = Registry.LocalMachine.OpenSubKey(openKey);
  30. // 谷歌浏览器就用谷歌打开,没找到就用系统默认的浏览器
  31. // 谷歌卸载了,注册表还没有清空,程序会返回一个"系统找不到指定的文件。"的bug
  32. if (appPath != null)
  33. {
  34. var result = Process.Start("chrome.exe", url);
  35. if (result == null)
  36. {
  37. OpenIe(url);
  38. }
  39. }
  40. else
  41. {
  42. var result = Process.Start("chrome.exe", url);
  43. if (result == null)
  44. {
  45. OpenDefaultBrowserUrl(url);
  46. }
  47. }
  48. }
  49. catch
  50. {
  51. // 出错调用用户默认设置的浏览器,还不行就调用IE
  52. OpenDefaultBrowserUrl(url);
  53. }
  54. }
  55. /// <summary>
  56. /// 用IE打开浏览器
  57. /// </summary>
  58. /// <param name="url"></param>
  59. public static void OpenIe(string url)
  60. {
  61. try
  62. {
  63. Process.Start("iexplore.exe", url);
  64. }
  65. catch (Exception ex)
  66. {
  67. //MessageBox.Show(ex.Message);
  68. // IE浏览器路径安装:C:\Program Files\Internet Explorer
  69. // at System.Diagnostics.process.StartWithshellExecuteEx(ProcessStartInfo startInfo)注意这个错误
  70. try
  71. {
  72. if (File.Exists(@"C:\Program Files\Internet Explorer\iexplore.exe"))
  73. {
  74. ProcessStartInfo processStartInfo = new ProcessStartInfo
  75. {
  76. FileName = @"C:\Program Files\Internet Explorer\iexplore.exe",
  77. Arguments = url,
  78. UseShellExecute = false,
  79. CreateNoWindow = true
  80. };
  81. Process.Start(processStartInfo);
  82. }
  83. else
  84. {
  85. if (File.Exists(@"C:\Program Files (x86)\Internet Explorer\iexplore.exe"))
  86. {
  87. ProcessStartInfo processStartInfo = new ProcessStartInfo
  88. {
  89. FileName = @"C:\Program Files (x86)\Internet Explorer\iexplore.exe",
  90. Arguments = url,
  91. UseShellExecute = false,
  92. CreateNoWindow = true
  93. };
  94. Process.Start(processStartInfo);
  95. }
  96. else
  97. {
  98. //if (MessageBox.Show(@"系统未安装IE浏览器,是否下载安装?",null,MessageBoxButtons.YesNoCancel,MessageBoxIcon.Question) == DialogResult.Yes)
  99. //{
  100. // // 打开下载链接,从微软官网下载
  101. // OpenDefaultBrowserUrl("http://windows.microsoft.com/zh-cn/internet-explorer/download-ie");
  102. //}
  103. }
  104. }
  105. }
  106. catch (Exception exception)
  107. {
  108. // MessageBox.Show(exception.Message);
  109. }
  110. }
  111. }
  112. /// <summary>
  113. /// 打开系统默认浏览器(用户自己设置了默认浏览器)
  114. /// </summary>
  115. /// <param name="url"></param>
  116. public static void OpenDefaultBrowserUrl(string url)
  117. {
  118. try
  119. {
  120. // 方法1
  121. //从注册表中读取默认浏览器可执行文件路径
  122. RegistryKey key = Registry.ClassesRoot.OpenSubKey(@"http\shell\open\command\");
  123. if (key != null)
  124. {
  125. string s = key.GetValue("").ToString();
  126. //s就是你的默认浏览器,不过后面带了参数,把它截去,不过需要注意的是:不同的浏览器后面的参数不一样!
  127. //"D:\Program Files (x86)\Google\Chrome\Application\chrome.exe" -- "%1"
  128. var lastIndex = s.IndexOf(".exe", StringComparison.Ordinal);
  129. if (lastIndex == -1)
  130. {
  131. lastIndex = s.IndexOf(".EXE", StringComparison.Ordinal);
  132. }
  133. var path = s.Substring(1, lastIndex + 3);
  134. var result = Process.Start(path, url);
  135. if (result == null)
  136. {
  137. // 方法2
  138. // 调用系统默认的浏览器
  139. var result1 = Process.Start("explorer.exe", url);
  140. if (result1 == null)
  141. {
  142. // 方法3
  143. Process.Start(url);
  144. }
  145. }
  146. }
  147. else
  148. {
  149. // 方法2
  150. // 调用系统默认的浏览器
  151. var result1 = Process.Start("explorer.exe", url);
  152. if (result1 == null)
  153. {
  154. // 方法3
  155. Process.Start(url);
  156. }
  157. }
  158. }
  159. catch
  160. {
  161. OpenIe(url);
  162. }
  163. }
  164. /// <summary>
  165. /// 火狐浏览器打开网页
  166. /// </summary>
  167. /// <param name="url"></param>
  168. public static void OpenFireFox(string url)
  169. {
  170. try
  171. {
  172. // 64位注册表路径
  173. var openKey = @"SOFTWARE\Wow6432Node\Mozilla\Mozilla Firefox";
  174. if (IntPtr.Size == 4)
  175. {
  176. // 32位注册表路径
  177. openKey = @"SOFTWARE\Mozilla\Mozilla Firefox";
  178. }
  179. RegistryKey appPath = Registry.LocalMachine.OpenSubKey(openKey);
  180. if (appPath != null)
  181. {
  182. var result = Process.Start("firefox.exe", url);
  183. if (result == null)
  184. {
  185. OpenIe(url);
  186. }
  187. }
  188. else
  189. {
  190. var result = Process.Start("firefox.exe", url);
  191. if (result == null)
  192. {
  193. OpenDefaultBrowserUrl(url);
  194. }
  195. }
  196. }
  197. catch
  198. {
  199. OpenDefaultBrowserUrl(url);
  200. }
  201. }
  202. }
  203. }