示例文件:DB.config
1.读取
1 //先实例化一个ExeConfigurationFileMap对象,把物理地址赋值到它的 ExeConfigFilename 属性中; 2 ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap(); 3 fileMap.ExeConfigFilename = @"DB.config"; 4 //再调用fileMap 实例化 config , 这样,操作的文件就是db.config文件了,也不会产生副本文件 5 Configuration config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None); 6 //获取appsettings节点 7 AppSettingsSection appsection = (AppSettingsSection)config.GetSection("appSettings"); 8 string conStrAll = appsection.Settings[ConnectionName].Value;
2.新增
1 //在appsettings节点中添加元素 2 appsection.Settings.Add( "addkey1 ", "key1 ‘s value "); 3 appsection.Settings.Add( "addkey2 ", "key2 ‘s value "); 4 config.Save();
3.删除
1 //删除appsettings节点中的元素 2 appsection.Settings.Remove( "addkey1 "); 3 config.Save();
4.修改
1 //修改appsettings节点中的元素 2 appsection.Settings[ "addkey2 "].Value = "modify key2 ‘s value "; 3 config.Save();
时间: 2024-11-09 02:02:11