读配置文件 方法(1)
//ConfigurationManager.RefreshSection("appSettings");
string settingValue = ConfigurationManager.AppSettings.Get("setting1");
读配置文件 方法(2)
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
string settingValue = config.AppSettings.Settings["setting1"].Value;
写配置文件
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings["setting1"].Value = "123";
config.Save(ConfigurationSaveMode.Modified);
App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="setting1" value="123" />
</appSettings>
</configuration>
注:不要试图用这种方法来写配置文件,这种方法根本写不了配置文件的:
ConfigurationManager.AppSettings.Set("setting1", "123");
时间: 2024-12-29 11:35:41