.NET下对Web.config与App.Config的增删改操作的代码

把代码过程常用的内容做个收藏,下边代码段是关于 .NET下对Web.config与App.Config的增删改操作的代码。
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="status" value="0" />
<add key="checkPath" value="C:" />
</appSettings>
</configuration>

看相关的操作:Web.config(ASP.NET):

System.Web.Configuration.WebConfigurationManager.AppSettings[“y”];
Configuration config = WebConfigurationManager.OpenWebConfiguration(null);
AppSettingsSection app = config.AppSettings;
app.Settings.Add("x", "this is X");
config.Save(ConfigurationSaveMode.Modified);
Configuration config = WebConfigurationManager.OpenWebConfiguration(null);
AppSettingsSection app = config.AppSettings;
app.Settings["x"].Value = "this is not Y";
config.Save(ConfigurationSaveMode.Modified);
Configuration config = WebConfigurationManager.OpenWebConfiguration(null);
AppSettingsSection app = config.AppSettings;
app.Settings.Remove("x");
config.Save(ConfigurationSaveMode.Modified);

App.config(WINFORM):

System.Configuration.ConfigurationManager.AppSettings[“y”];
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
AppSettingsSection app = config.AppSettings;
app.Settings.Add("x", "this is X");
config.Save(ConfigurationSaveMode.Modified);
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
AppSettingsSection app = config.AppSettings;
app.Settings["x"].Value = "this is not Y";
config.Save(ConfigurationSaveMode.Modified);
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
AppSettingsSection app = config.AppSettings;
app.Settings.Remove("x");
config.Save(ConfigurationSaveMode.Modified);

上一篇:把lighttpd配置为系统服务


下一篇:利用Node.js对某智能家居服务器重构