app.config ConfigSection 保护
public void ProtectSection(string sectionName)
{
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
ConfigurationSection protectedSection= config.GetSection(sectionName);
// Encrypts when possible
if ((protectedSection!=null) && (!protectedSection.IsReadOnly()) && (! protectedSection.SectionInformation.IsProtected)
&& (!protectedSection.SectionInformation.IsLocked) && (protectedSection.SectionInformation.IsDeclared))
{
// Protect (encrypt)the section.
protectedSection.SectionInformation.ProtectSection(null);
// ' Save the encrypted section.
protectedSection.SectionInformation.ForceSave =true;
config.Save(ConfigurationSaveMode.Full);
}
}
{
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
ConfigurationSection protectedSection= config.GetSection(sectionName);
// Encrypts when possible
if ((protectedSection!=null) && (!protectedSection.IsReadOnly()) && (! protectedSection.SectionInformation.IsProtected)
&& (!protectedSection.SectionInformation.IsLocked) && (protectedSection.SectionInformation.IsDeclared))
{
// Protect (encrypt)the section.
protectedSection.SectionInformation.ProtectSection(null);
// ' Save the encrypted section.
protectedSection.SectionInformation.ForceSave =true;
config.Save(ConfigurationSaveMode.Full);
}
}