private static string _strProvider = "RsaProtectedConfigurationProvider"; /// <summary> /// 提供加密的驱动 /// </summary> public string Provider { get { return _strProvider; } set { _strProvider = value; } } /// <summary> /// /// </summary> public ConfigSection() { } /// <summary> /// 加密配置文件节点 /// </summary> /// <param name="strSectionName">节点名称</param> public static void EncryptConfigSection(string strSectionName) { Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); ConfigurationSection section = config.GetSection(strSectionName); if (section != null && !section.IsReadOnly() && !section.ElementInformation.IsLocked && !section.SectionInformation.IsProtected) { section.SectionInformation.ProtectSection(_strProvider); section.SectionInformation.ForceSave = true; config.Save(ConfigurationSaveMode.Full); ConfigurationManager.RefreshSection(strSectionName); } } /// <summary> /// 解密配置文件节点 /// </summary> /// <param name="strSectionName">节点名称</param> public static void DecryptConfigSection(string strSectionName) { Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); ConfigurationSection section = config.GetSection(strSectionName); if (section != null && !section.IsReadOnly() && !section.ElementInformation.IsLocked && section.SectionInformation.IsProtected) { section.SectionInformation.UnprotectSection(); section.SectionInformation.ForceSave = true; config.Save(ConfigurationSaveMode.Full); ConfigurationManager.RefreshSection(strSectionName); } }