protected override void OnStartup(StartupEventArgs e) { base.OnStartup(e); Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); var appsettings = config.AppSettings.Settings; if (appsettings["FirstRun"].Value == "true") { mRegistryHelper.OpeningSettingPage += delegate { }; mRegistryHelper.Register(); BrowserRegistry bR = mRegistryHelper.UserBrowserRegistry; BrowserRegistry oR = mRegistryHelper.osiBrowserRegistry; if (oR.http_Hash != null) { appsettings["HttpHash"].Value = oR.http_Hash; } if (oR.https_Hash != null) { appsettings["HttpsHash"].Value = oR.https_Hash; } appsettings["FirstRun"].Value = "false"; } else { mRegistryHelper.Register(new osiBrowserRegistry(appsettings["HttpHash"].Value, appsettings["HttpsHash"].Value)); } config.Save(); Current.MainWindow = new MainWindow(); Current.MainWindow.Show(); }
/// <summary> /// 注册为默认浏览器 /// </summary> /// <param name="registry"></param> public void Register(BrowserRegistry registry = null) { /// 两个方案 /// 1.Windows_10_Above_And_Include_Build10122 将osi注册为默认浏览器并 *不修改* 回原浏览器 /// 2.Windows_10_Below_Build10122 Windows_8_And_Above Windows_7_And_Vista 将osi注册为默认浏览器并 *修改* 回原浏览器 bool IsFirstRun = false; if (registry == null) { IsFirstRun = true; registry = osiBrowserRegistry; } //Is First Run And Check OSVersion if (IsFirstRun && (osVersion == OSVersion.Windows_10_Above_And_Include_Build10122 || osVersion == OSVersion.Windows_10_Below_Build10122 || osVersion == OSVersion.Windows_8_And_Above || osVersion == OSVersion.Windows_7_And_Vista)) { RegisterAsBrowser(); } GetUserBrowserRegistry(); RegisterAsDefaultBrowser(registry); if (IsFirstRun) { if (osVersion == OSVersion.Windows_10_Above_And_Include_Build10122 || osVersion == OSVersion.Windows_10_Below_Build10122 || osVersion == OSVersion.Windows_8_And_Above || osVersion == OSVersion.Windows_7_And_Vista) { GetHash(); } else { osiBrowserRegistry.http_Hash = null; osiBrowserRegistry.https_Hash = null; } } }
/// <summary> /// 注册为默认浏览器 /// </summary> /// <param name="HttpHash"></param> /// <param name="HttpsHash"></param> private void RegisterAsDefaultBrowser(BrowserRegistry registry) { if (osVersion == OSVersion.Windows_10_Above_And_Include_Build10122) { //No thing to do } if (osVersion == OSVersion.Windows_10_Above_And_Include_Build10122 || osVersion == OSVersion.Windows_10_Below_Build10122 || osVersion == OSVersion.Windows_8_And_Above || osVersion == OSVersion.Windows_7_And_Vista) { RegistryKey httpUserChoice = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http\UserChoice", true); RegistryKey httpsUserChoice = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\Shell\Associations\UrlAssociations\https\UserChoice", true); httpUserChoice.SetValue("Progid", registry.http_Progid); httpsUserChoice.SetValue("Progid", registry.https_Progid); if (registry.http_Hash != null && registry.https_Hash != null) { httpUserChoice.SetValue("Hash", registry.http_Hash); httpsUserChoice.SetValue("Hash", registry.https_Hash); } } else if (osVersion == OSVersion.Windows_2000_And_Xp) { RegistryKey http = Registry.CurrentUser.OpenSubKey(@"Software\Classes\http", true); http.OpenSubKey("DefaultIcon", true).SetValue("", registry.http_ApplicationIconPath); http.OpenSubKey(@"shell\open\command", true).SetValue("", registry.http_Command); http.Close(); RegistryKey https = Registry.CurrentUser.OpenSubKey(@"Software\Classes\https", true); https.OpenSubKey("DefaultIcon", true).SetValue("", registry.https_ApplicationIconPath); https.OpenSubKey(@"shell\open\command", true).SetValue("", registry.https_Command); https.Close(); } else { throw new Exception("Unsupported"); } }
/// <summary> /// 恢复用户默认浏览器设置 /// </summary> /// <param name="registry"></param> public void UnRegister(BrowserRegistry registry) { Register(UserBrowserRegistry); }