终端一体化运控平台
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.

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