检查电脑本地IE 证书等操作 注册表辅助类
/// <summary>
/// 注册表辅助类
/// </summary>
public class RegistryHelper { #region 需要权限才可以操作 private const string SoftName = "OfficeConvertService";//您的软件所用到的注册表节点,所有键值对将在该节下创建 /// <summary> /// 取得注册表值 需要权限 /// </summary> /// <param name="name"></param> /// <returns></returns> public static string GetKeyValue(string name) { RegistryKey hkml = Registry.LocalMachine; RegistryKey software = hkml.OpenSubKey("SOFTWARE", true); if (software != null) { RegistryKey subKeys = software.OpenSubKey(SoftName, true); if (subKeys != null) { string registData = subKeys.GetValue(name).ToString(); return registData; } } return string.Empty; } /// <summary> /// 添加注册表键值对 需要权限 /// </summary> /// <param name="key"></param> /// <param name="keyValue"></param> public static void AddKey(string key, string keyValue) { RegistryKey hklm = Registry.LocalMachine; RegistryKey software = hklm.OpenSubKey("SOFTWARE", true); if (software != null) { RegistryKey subKey = software.CreateSubKey(SoftName); if (subKey != null) subKey.SetValue(key, keyValue); } } /// <summary> /// 删除注册表键 需要权限 /// </summary> /// <param name="key"></param> public static void DeleteKey(string key) { RegistryKey hkml = Registry.LocalMachine; RegistryKey software = hkml.OpenSubKey("SOFTWARE", true); if (software != null) { RegistryKey subKey = software.OpenSubKey(SoftName, true); if (subKey != null) { string[] aimnames = subKey.GetSubKeyNames(); foreach (string aimKey in aimnames) { if (aimKey == key) subKey.DeleteSubKeyTree(key); } } } } /// <summary> /// 检查注册表是否存在键 需要权限 /// </summary> /// <param name="key"></param> /// <returns></returns> public static bool IsRegisted(string key) { try { RegistryKey hkml = Registry.LocalMachine; RegistryKey software = hkml.OpenSubKey("SOFTWARE", true); if (software != null) { RegistryKey subKeys = software.OpenSubKey(SoftName, true); if (subKeys != null) { string[] keyNames = subKeys.GetValueNames(); foreach (string keyName in keyNames) { if (keyName == key) { return true; } } } } } catch { return false; } return false; } #endregion /// <summary> /// 检查服务器证书是否吊销 /// </summary> /// <param name="isOpen">1开启,0关闭</param> public void SetCertificateVerificationState(bool isOpen) { var internetSettings = @"Software\Microsoft\Windows\CurrentVersion\Internet Settings"; RegistryHelper.ModifyCurrentUserRegistryKey(internetSettings, "CertificateRevocation", isOpen ? "1" : "0"); } /// <summary> /// 检查发行商的证书是否吊销 /// </summary> /// <param name="isOpen"> 0 开启,512 取消</param> public void SetCertificatePublishingState(bool isOpen) { var softwarePublishing = @"Software\Microsoft\Windows\CurrentVersion\WinTrust\Trust Providers\Software Publishing"; RegistryHelper.ModifyCurrentUserRegistryKey(softwarePublishing, "State", isOpen ? "0" : "512 "); } /// <summary> /// 修改IE安全等级为中 /// </summary> public void SetInternetProtectLevelNormal() { //"CurrentLevel"=dword:00011000 //"MinLevel" = dword:00011000 //"RecommendedLevel" = dword:00011000 var internetSettings = @"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Zones\3"; RegistryHelper.ModifyCurrentUserRegistryKey(internetSettings, "CurrentLevel", "00011000"); RegistryHelper.ModifyCurrentUserRegistryKey(internetSettings, "MinLevel", "00011000"); RegistryHelper.ModifyCurrentUserRegistryKey(internetSettings, "RecommendedLevel", "00011000"); } /// <summary> /// 设置SSL And TSL 是否勾选 /// </summary> /// <param name="isOpen"></param> public static void SetSSLAndTSLState(bool isOpen) { //使用SSL3.0和TLS1.0 var internetSettings = @"Software\Microsoft\Windows\CurrentVersion\Internet Settings"; RegistryHelper.ModifyCurrentUserRegistryKey(internetSettings, "SecureProtocols", isOpen ? "2688" : "0"); } /// <summary> /// 注册表修改 /// </summary> /// <param name="registerPath">注册表地址</param> /// <param name="key">键</param> /// <param name="value">值</param> /// <returns></returns> public static bool ModifyCurrentUserRegistryKey(string registerPath, string key, string value) { RegistryKey currentUserKey = null; RegistryKey subKey = null; try { currentUserKey = Registry.CurrentUser; subKey = GetSubKey(currentUserKey, registerPath); if (subKey != null) { subKey.SetValue(key, value, RegistryValueKind.DWord); subKey.Close(); subKey.Dispose(); } } catch (Exception e) { subKey?.Close(); subKey?.Dispose(); return false; } currentUserKey?.Close(); currentUserKey?.Dispose(); return true; } /// <summary> /// 检查注册表是否存在 /// </summary> /// <param name="currentUserKey">注册键</param> /// <param name="registerPath">注册表地址</param> /// <returns></returns> private static RegistryKey GetSubKey(RegistryKey currentUserKey, string registerPath) { RegistryKey subKey; subKey = currentUserKey.OpenSubKey(registerPath, true); if (subKey == null) { subKey = currentUserKey.CreateSubKey(registerPath, RegistryKeyPermissionCheck.ReadWriteSubTree); } return subKey; } // <summary> /// 重置IE浏览器 /// </summary> public void ResetInternetSetting() { var internetSettings = @"Software\Microsoft\Internet Explorer"; RegistryHelper.DeleteCurrentUserRegistryPath(internetSettings); } /// <summary> /// 重置IE浏览器 /// </summary> /// <param name="registerPath"></param> /// <returns></returns> public static bool DeleteCurrentUserRegistryPath(string registerPath) { RegistryKey currentUserKey = null; try { currentUserKey = Registry.CurrentUser; currentUserKey.DeleteSubKey(registerPath, false); } catch (Exception e) { return false; } currentUserKey?.Close(); currentUserKey?.Dispose(); return true; } }