WebBrowser.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // /* **********************************************************************************
  2. // *
  3. // * Copyright (c) Sky Sanders. All rights reserved.
  4. // *
  5. // * This source code is subject to terms and conditions of the Microsoft Public
  6. // * License (Ms-PL). A copy of the license can be found in the license.htm file
  7. // * included in this distribution.
  8. // *
  9. // * You must not remove this notice, or any other, from this software.
  10. // *
  11. // * **********************************************************************************/
  12. using System;
  13. using System.IO;
  14. using Microsoft.Win32;
  15. namespace CassiniDev
  16. {
  17. /// <summary>
  18. /// A simple user agent locator - derived from Nikhil Kothari's Script#
  19. /// http://projects.nikhilk.net/ScriptSharp
  20. /// I would have written it, but how many ways are there to get paths from
  21. /// the registry?
  22. ///
  23. /// TODO: Add Opera
  24. /// </summary>
  25. public sealed class WebBrowser
  26. {
  27. private readonly string _executablePath;
  28. private readonly string _name;
  29. public static readonly WebBrowser Chrome = new WebBrowser("Chrome", GetChromeExecutablePath());
  30. public static readonly WebBrowser Firefox = new WebBrowser("Firefox", GetFirefoxExecutablePath());
  31. public static readonly WebBrowser InternetExplorer = new WebBrowser("Internet Explorer", "iexplore.exe");
  32. public static readonly WebBrowser Safari = new WebBrowser("Safari", GetSafariExecutablePath());
  33. public static readonly WebBrowser Opera = new WebBrowser("Opera", GetOperaExecutablePath());
  34. //HKEY_CURRENT_USER\Software\Opera Software
  35. private WebBrowser(string name, string executablePath)
  36. {
  37. _name = name;
  38. _executablePath = executablePath;
  39. }
  40. private static string GetOperaExecutablePath()
  41. {
  42. string path = null;
  43. RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Software\Opera Software\");
  44. if (key != null)
  45. {
  46. path = (string)key.GetValue("Last CommandLine v2");
  47. if (!File.Exists(path))
  48. {
  49. path = null;
  50. }
  51. }
  52. return path;
  53. }
  54. private static string GetChromeExecutablePath()
  55. {
  56. string path = null;
  57. RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Uninstall\Google Chrome\");
  58. if (key != null)
  59. {
  60. path = Path.Combine((string)key.GetValue("InstallLocation"), "chrome.exe");
  61. if (!File.Exists(path))
  62. {
  63. path = null;
  64. }
  65. }
  66. return path;
  67. }
  68. private static string GetFirefoxExecutablePath()
  69. {
  70. string path = null;
  71. RegistryKey key = Registry.LocalMachine.OpenSubKey(@"Software\Mozilla\Mozilla Firefox");
  72. if (key != null)
  73. {
  74. var str2 = (string)key.GetValue("CurrentVersion");
  75. if (!string.IsNullOrEmpty(str2))
  76. {
  77. RegistryKey key2 = key.OpenSubKey(string.Format(@"{0}\Main", str2));
  78. if (key2 == null)
  79. {
  80. return path;
  81. }
  82. path = (string)key2.GetValue("PathToExe");
  83. if (!File.Exists(path))
  84. {
  85. path = null;
  86. }
  87. }
  88. return path;
  89. }
  90. string str3 = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), @"Mozilla FireFox\FireFox.exe");
  91. if (File.Exists(str3))
  92. {
  93. return str3;
  94. }
  95. str3 = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles) + " (x86)", @"Mozilla FireFox\FireFox.exe");
  96. if (!File.Exists(str3))
  97. {
  98. path = str3;
  99. }
  100. return path;
  101. }
  102. private static string GetSafariExecutablePath()
  103. {
  104. string path = null;
  105. RegistryKey key = Registry.LocalMachine.OpenSubKey(@"Software\Apple Computer, Inc.\Safari");
  106. if (key != null)
  107. {
  108. path = (string)key.GetValue("BrowserExe");
  109. if (!File.Exists(path))
  110. {
  111. path = null;
  112. }
  113. }
  114. return path;
  115. }
  116. internal string ExecutablePath
  117. {
  118. get
  119. {
  120. return _executablePath;
  121. }
  122. }
  123. public string Name
  124. {
  125. get
  126. {
  127. return _name;
  128. }
  129. }
  130. }
  131. }