博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
怎么通过Configuration 类编辑配置文件
阅读量:4313 次
发布时间:2019-06-06

本文共 1920 字,大约阅读时间需要 6 分钟。

代码如下:

        //编辑web.config文件
        //打开配置文件
        Configuration   config   =   System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration( "~ ");
        //获取appsettings节点
        AppSettingsSection   appsection   =   (AppSettingsSection)config.GetSection( "appSettings ");
        //在appsettings节点中添加元素
        appsection.Settings.Add( "addkey1 ",   "key1 's   value ");
        appsection.Settings.Add( "addkey2 ",   "key2 's   value ");
        config.Save();
        //删除节点或属性
        //打开配置文件
        Configuration   config   =   System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration( "~ ");
        //获取appsettings节点
        AppSettingsSection   appsection   =   (AppSettingsSection)config.GetSection( "appSettings ");
        //删除appsettings节点中的元素
        appsection.Settings.Remove( "addkey1 ");
        //修改appsettings节点中的元素
        appsection.Settings[ "addkey2 "].Value   =   "modify   key2 's   value ";
        config.Save();
       
        //编辑App.config文件
        ExeConfigurationFileMap   file   =   new   ExeConfigurationFileMap();
        file.ExeConfigFilename   =   @ "..\..\test.config ";
        //打开配置文件
        Configuration   myConfig   =   System.Configuration.ConfigurationManager.OpenMappedExeConfiguration(file,   ConfigurationUserLevel.None);
        //获取appsettings节点
        AppSettingsSection   appsection   =   (AppSettingsSection)myConfig.GetSection( "appSettings ");
        //在appsettings节点中添加元素
        appsection.Settings.Add( "addkey1 ",   "key1 's   value ");
        appsection.Settings.Add( "addkey2 ",   "key2 's   value ");
        config.Save();
        //删除节点或属性
        //打开配置文件
        ExeConfigurationFileMap   file   =   new   ExeConfigurationFileMap();
        file.ExeConfigFilename   =   @ "..\..\test.config ";
        //打开配置文件
        Configuration   myConfig   =   System.Configuration.ConfigurationManager.OpenMappedExeConfiguration(file,   ConfigurationUserLevel.None);
        //获取appsettings节点
        AppSettingsSection   appsection   =   (AppSettingsSection)myConfig.GetSection( "appSettings ");
        //删除appsettings节点中的元素
        appsection.Settings.Remove( "addkey1 ");
        //修改appsettings节点中的元素
        appsection.Settings[ "addkey2 "].Value   =   "modify   key2 's   value ";
        myConfig.Save();

转载于:https://www.cnblogs.com/love-summer/archive/2011/11/09/2242244.html

你可能感兴趣的文章
去掉PowerDesigner生成SQL脚本中字段名带的引号
查看>>
win10操作系统安装oracle11g时出现不满足最低配置的操作INS13001
查看>>
java基础学习——7、String类和StringBuffer类的区别
查看>>
js基础
查看>>
sklearn 中 make_blobs模块
查看>>
python学习笔记之多个装饰器
查看>>
自己创建一个android studio在线依赖compile
查看>>
【android】安卓开发apk列表
查看>>
JavaScript高级特性-实现继承的七种方式
查看>>
20121016学习笔记四
查看>>
EntityFramework 学习 一 Stored Procedure
查看>>
Sliverlight之 故事板
查看>>
Java 必知必会的 20 种常用类库和 API
查看>>
HDU 1087 Super Jumping! Jumping! Jumping!
查看>>
0007_初始模块和字节码
查看>>
[效率提升]如何管理好你的电脑文件
查看>>
C++实验二
查看>>
Sultan's Dowry Problem - 苏丹新娘问题
查看>>
SharePoint2010 富文本框添加图片功能的扩展
查看>>
零零碎碎的知识
查看>>