Configuration objConfig = ConfigurationManager.OpenExeConfiguration(System.Windows.Forms.Application.StartupPath + @"\TestConfig.exe");
//需要有TestConfig.exe文件
ConnectionStringsSection objAppsettings = null;
objAppsettings = (ConnectionStringsSection)objConfig.GetSection("connectionStrings");
foreach (ConnectionStringSettings s in objAppsettings.ConnectionStrings)
{
Console.WriteLine(s.Name);
if (s.Name == "ConnectionString")
{
string ss;
if (s.LockItem == false)
{
ss = s.ConnectionString;
s.ConnectionString += " encrypt";
s.LockItem = true;
objConfig.Save(ConfigurationSaveMode.Modified);
}
else
{
ss = s.ConnectionString;
}
Console.WriteLine(ss);
}
}
Console.ReadKey();
if (!objAppsettings.SectionInformation.IsProtected)
{
objAppsettings.SectionInformation.ProtectSection("RsaProtectedConfigurationProvider");
objAppsettings.SectionInformation.ForceSave = true;
objConfig.Save(ConfigurationSaveMode.Modified);
}
else
{
objAppsettings.SectionInformation.UnprotectSection();
objAppsettings.SectionInformation.ForceSave = true;
objConfig.Save(ConfigurationSaveMode.Modified);
}