BrowserHelper.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. using Microsoft.Win32;
  2. using System.Diagnostics;
  3. namespace Client.Utils
  4. {
  5. public class BrowserHelper
  6. {
  7. private static void OpenChrometBrowser(string url)
  8. {
  9. try
  10. {
  11. var openKey = @"SOFTWARE\Wow6432Node\Google\Chrome";
  12. if (IntPtr.Size == 4)
  13. {
  14. openKey = @"SOFTWARE\Google\Chrome";
  15. }
  16. RegistryKey appPath = Registry.LocalMachine.OpenSubKey(openKey);
  17. if (appPath != null)
  18. {
  19. var result = Process.Start("chrome.exe", url);
  20. if (result == null)
  21. {
  22. OpenIetBrowser(url);
  23. }
  24. }
  25. else
  26. {
  27. OpenDefaultBrowser(url);
  28. }
  29. }
  30. catch
  31. {
  32. OpenDefaultBrowser(url);
  33. }
  34. }
  35. private static void OpenIetBrowser(string url)
  36. {
  37. try
  38. {
  39. Process.Start("iexplore.exe", url);
  40. }
  41. catch (Exception ex)
  42. {
  43. MessageBox.Show(ex.Message);
  44. try
  45. {
  46. if (File.Exists(@"C:\Program Files\Internet Explorer\iexplore.exe"))
  47. {
  48. ProcessStartInfo processStartInfo = new ProcessStartInfo
  49. {
  50. FileName = @"C:\Program Files\Internet Explorer\iexplore.exe",
  51. Arguments = url,
  52. UseShellExecute = false,
  53. CreateNoWindow = true
  54. };
  55. Process.Start(processStartInfo);
  56. }
  57. else
  58. {
  59. if (File.Exists(@"C:\Program Files (x86)\Internet Explorer\iexplore.exe"))
  60. {
  61. ProcessStartInfo processStartInfo = new ProcessStartInfo
  62. {
  63. FileName = @"C:\Program Files (x86)\Internet Explorer\iexplore.exe",
  64. Arguments = url,
  65. UseShellExecute = false,
  66. CreateNoWindow = true
  67. };
  68. Process.Start(processStartInfo);
  69. }
  70. else
  71. {
  72. if (MessageBox.Show("Internet Explorer is not installed in the system. Do you want to download and install it?", null, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question) == DialogResult.Yes)
  73. {
  74. OpenDefaultBrowser("http://windows.microsoft.com/zh-cn/internet-explorer/download-ie");
  75. }
  76. }
  77. }
  78. }
  79. catch (Exception exception)
  80. {
  81. MessageBox.Show(exception.Message);
  82. }
  83. }
  84. }
  85. public static void OpenDefaultBrowser(string url)
  86. {
  87. try
  88. {
  89. RegistryKey key = Registry.ClassesRoot.OpenSubKey(@"http\shell\open\command\");
  90. if (key != null)
  91. {
  92. string s = key.GetValue("").ToString();
  93. var lastIndex = s.IndexOf(".exe", StringComparison.Ordinal);
  94. var path = s.Substring(1, lastIndex + 3);
  95. var result = Process.Start(path, url);
  96. if (result == null)
  97. {
  98. var result1 = Process.Start("explorer.exe", url);
  99. if (result1 == null)
  100. {
  101. Process.Start(url);
  102. }
  103. }
  104. }
  105. else
  106. {
  107. var result1 = Process.Start("explorer.exe", url);
  108. if (result1 == null)
  109. {
  110. Process.Start(url);
  111. }
  112. }
  113. }
  114. catch
  115. {
  116. OpenIetBrowser(url);
  117. }
  118. }
  119. }
  120. }