.NET 2.0之后,Web.config和App.Config的增删改操作是相当的方便,使用好了能达到事半功倍的效果。下面是一个App.Config,其实就是一个XML文件
<?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.Add("x", "this is X"); 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); //ConfigurationManager.RefreshSection("appSettings");
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.Add("x", "this is X"); 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); //ConfigurationManager.RefreshSection("appSettings");