1.隨著IE9之後,IE8和IE10獲取瀏覽器的版本發生了變化,裝了IE10,IE11后註冊表Version的值都是9,@”SOFTWARE\Microsoft\Internet Explorer\Version Vector”的版本也是9,svcVersion值才是正確的版本
2.默認瀏覽器的安裝路徑,目前也不統一,瀏覽器自帶的設定默認瀏覽器可能也是無效的,註冊表:Software\Clients\StartmenuInternet\和http\shell\open\command地方會記錄,瀏覽器自帶設定有的會修改兩處,有的只修改了其中一處,所以判定也不是很準,大家自行決定,Win7設定前者值,XP電腦修改了兩處,QQ瀏覽器設置的後者
3.獲取主流瀏覽器的安裝路徑,大部分安裝程式會在註冊表@”SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths”里,也存在一些列外,只在自己的註冊表里記錄,此處測試了 chrome,360,baidubrowser,firefox,iexplore,ucbrowser,sogouexplorer.exe,qqbrowser,Opera
`
public class PubString { /// <summary> /// 獲取IE瀏覽器版本 IE10和IE8的方式不同 /// </summary> /// <returns></returns> public static string GetIEVer() { try { RegistryKey mainKey = Microsoft.Win32.Registry.LocalMachine; RegistryKey subKey = mainKey.OpenSubKey(@"SOFTWARE\Microsoft\Internet Explorer"); string IEVersion = PubString.ToString(subKey.GetValue("svcVersion"));//IE9+ //IE9-以下方式也可以 //subKey = mainKey.OpenSubKey(@"SOFTWARE\Microsoft\Internet Explorer\Version Vector"); //IEVersion = (string)subKey.GetValue("IE"); if (IEVersion == "") { IEVersion = (string)subKey.GetValue("Version");//IE9-- } return IEVersion; } catch (System.Exception ex) { throw ex; } } /// <summary> /// 獲取默認瀏覽器的路徑 ,返回結果如下,部分瀏覽器不准 /// "C:\Program Files\UCBrowser\Application\UCBrowser.exe" /////SogouExplorer.exe /////360安全浏览器 /////"C:\Users\Administrator\AppData\Local\Google\Chrome\Application\chrome.exe" /////"C:\Program Files\Opera developer\Launcher.exe" /////C:\Program Files\Internet Explorer\iexplore.exe /////"C:\Users\Administrator\AppData\Local\Baidu\BaiduBrowser\8.7.5000.4969\baidubrowser.exe" /////"C:\Program Files\Mozilla Firefox\firefox.exe" /// </summary> /// <returns></returns> public static string GetDefaultBrowserPath() { string _BrowserKey1 = @"Software\Clients\StartmenuInternet\"; string _BrowserKey2 = @"\shell\open\command"; RegistryKey _RegistryKey = Registry.CurrentUser.OpenSubKey(_BrowserKey1, false); if (_RegistryKey == null) _RegistryKey = Registry.LocalMachine.OpenSubKey(_BrowserKey1, false); string _Result = PubString.ToString(_RegistryKey.GetValue(""));//Win10報錯 _RegistryKey.Close(); _RegistryKey = Registry.LocalMachine.OpenSubKey(_BrowserKey1 + _Result + _BrowserKey2); if (_RegistryKey != null) { _Result = PubString.ToString(_RegistryKey.GetValue("")); _RegistryKey.Close(); } if (_Result != "") { _Result = _Result.Replace("\"", "");//移出多餘的引號 } if (_Result == "" || (_Result != "" && (!File.Exists(_Result)))) { _RegistryKey = Registry.ClassesRoot.OpenSubKey("http\\shell\\open\\command", false); _Result = PubString.ToString(_RegistryKey.GetValue("")); if (_Result != "") { _Result = _Result.Substring(0,_Result.IndexOf(".exe")+4); } _RegistryKey.Close(); } if (_Result != "") { _Result = _Result.Replace("\"","");//移出多餘的引號 } if (!File.Exists(_Result)) { //路徑不存在,即取消 _Result = ""; } return _Result; } /// <summary> /// 獲取主流瀏覽器的 安裝路徑 /// </summary> /// <returns></returns> public static List<string> GetBrowsersPath() { List<string> Browsers = new List<string>(); try { //檢測瀏覽器 var openKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths"; RegistryKey appPath = Registry.LocalMachine.OpenSubKey(openKey, false); string[] apps = appPath.GetSubKeyNames(); foreach (string App in apps) { RegistryKey SubApp = appPath.OpenSubKey(App); if (SubApp != null) { string Path = PubString.ToString(SubApp.GetValue("")); if (Path != "" && Path.EndsWith("exe")) { switch (App.ToLower()) { case "chrome.exe": case "360se6.exe": case "baidubrowser.exe": case "firefox.exe": case "iexplore.exe": case "ucbrowser.exe": case "sogouexplorer.exe": case "qqbrowser.exe": if (File.Exists(Path)) { Browsers.Add(Path); } break; } } } } //以下是在 openKey = @"SOFTWARE\SogouExplorer"; appPath = Registry.CurrentUser.OpenSubKey(openKey, false); if (appPath != null && appPath.GetValue("") != null) { string Path = PubString.ToString(appPath.GetValue("")); if (Path.EndsWith("exe")) { if (File.Exists(Path)) { Browsers.Add(Path); } } else if (Path.Contains(@"\")) { Path = Path + @"\SogouExplorer.exe"; if (File.Exists(Path)) { Browsers.Add(Path); } } } openKey = @"SOFTWARE\Tencent\QQBrowser"; appPath = Registry.LocalMachine.OpenSubKey(openKey, false); if (appPath != null && appPath.GetValue("Exe") != null) { string Path = PubString.ToString(appPath.GetValue("Exe")); if (Path.EndsWith("exe")) { if (File.Exists(Path)) { Browsers.Add(Path); } } } openKey = @"SOFTWARE\Tencent\Opera Software"; appPath = Registry.CurrentUser.OpenSubKey(openKey, false); if (appPath != null && appPath.GetValue("Last developer Install Path") != null) { string Path = PubString.ToString(appPath.GetValue("Last developer Install Path")); if (Path.EndsWith("exe")) { if (File.Exists(Path)) { Browsers.Add(Path); } } else if (Path.Contains(@"\")) { Path = Path + @"\launcher.exe"; if (File.Exists(Path)) { Browsers.Add(Path); } } } } catch (System.Exception ex) { throw ex; } return Browsers; } /// <summary> /// 將null值變為空字符串 /// </summary> /// <param name="Str"></param> /// <returns></returns> public static string ToString(object Str) { try { if (Str == null) { return ""; } if (string.IsNullOrEmpty(Str.ToString())) { return ""; } else { return Convert.ToString(Str); } } catch (System.Exception ex) { throw ex; } } }