app.config/web.config配置文件增删改
一、概述
应用程序配置文件,对于asp.net是 web.config,对于WINFORM程序是 App.Config(ExeName.exe.config)。
配置文件,对于程序本身来说,就是基础和依据,其本质是一个xml文件,对于配置文件的操作,从.NET 2.0 开始,就非常方便了,提供了 System [.Web] .Configuration 这个管理功能的命名空间,要使用它,需要添加对 System.configuration.dll的引用。
- 对于WINFORM程序,使用 System.Configuration.ConfigurationManager;
- 对于ASP.NET 程序, 使用 System.Web.Configuration.WebConfigurationManager;
二、配置文件
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="y" value="this is Y"/>
</appSettings>
</configuration>
三、读取配置文件:
1. 读取值:
System.Configuration.ConfigurationManager.AppSettings[“y”];
Asp.Net程序:
System.Web.Configuration.WebConfigurationManager.AppSettings[“y”];
读取最新值:
Configuration config = ConfigurationManager.OpenExeConfiguration(null);
AppSettingsSection app = config.AppSettings;
// 或者AppSettingsSection app =config.GetSection("AppSettings") as AppSettingsSection
Asp.Net程序读取值:
Configuration config = WebConfigurationManager.OpenWebConfiguration(null);
AppSettingsSection app = config.AppSettings;
// 或者AppSettingsSection app =config.GetSection("AppSettings") as AppSettingsSection;
2、查看值
string y=app.Settings["y"].Value;
foreach (string key in app.Settings)
{
Console.WriteLine(key+","+ app.Settings[key].Value);
}
3、添加一项
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
AppSettingsSection app = config.AppSettings;
app.Settings.Add("x", "this is X");
config.Save(ConfigurationSaveMode.Modified);
4、修改一项
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
AppSettingsSection app = config.AppSettings;
//app.Settings.Add("x", "this is X");
app.Settings["x"].Value = "this is not Y";
config.Save(ConfigurationSaveMode.Modified);
5、删除一项
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
AppSettingsSection app = config.AppSettings;
app.Settings.Remove("x");
config.Save(ConfigurationSaveMode.Modified);
说明:需要注意的是,代码所修改的并不是app.config,而是[Application_Name].exe.config这个文件。
其中Application_Name就是你的可执行文件的文件名,而[Application_Name].exe.config才是真正起作用的配置文件。
至于app.config,把它理解为是初始化配置文件比较合适。
四、连接字符串
1、读取连接字符串
ConnectionStringSettings conn= ConfigurationManager.ConnectionStrings["y"];
Configuration config = ConfigurationManager.OpenExeConfiguration(null);
ConnectionStringsSection connSeciton = config.ConnectionStrings;
connSeciton.ConnectionStrings.Add(new ConnectionStringSettings("name", "connectionstring", "provider"));
2、加密字符串
public static void EncryptConnectionString(bool encrypt)
{
Configuration configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
ConnectionStringsSection configSection = configFile.GetSection("connectionStrings") as ConnectionStringsSection;
if ((!(configSection.ElementInformation.IsLocked)) && (!(configSection.SectionInformation.IsLocked)))
{
if (encrypt && !configSection.SectionInformation.IsProtected) //encrypt is false to unencrypt
{
configSection.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");
}
if (!encrypt && configSection.SectionInformation.IsProtected)
{
configSection.SectionInformation.UnprotectSection(); //encrypt is true so encrypt
}
configSection.SectionInformation.ForceSave = true; //re-save the configuration file section
configFile.Save(); // Save the current configuration.
}
}