.Net 对App.config和Web.config的访问操作(增、删、读、改)
一。首先引用Configuration
1)App.config如下:
using System.Configuration;//若果还没有Configuration,右键引用文件夹添加引用,在.NET中找到System.configuration添加
2)Web.config如下:
using System.Configuration;
using System.Web.Configuration;
1)App.config页面代码如下:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="name" value="张三"/>
</appSettings>
</configuration>
2)Web.config页面代码如下:
<?xml version="1.0"?>
<!--
有关如何配置 ASP.NET 应用程序的详细信息,请访问
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0"/>
</system.web>
<appSettings>
<add key="name" value="张三"/>
</appSettings>
</configuration>
二。增加
1)App.config示例:
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
AppSettingsSection appsettings = (AppSettingsSection)config.GetSection("appSettings");
appsettings.Settings.Add("color", "blue");
config.Save();
string str = ConfigurationManager.AppSettings["color"];
2)Web.config示例:
Configuration config = WebConfigurationManager.OpenWebConfiguration(null);
AppSettingsSection app = config.AppSettings;
app.Settings.Add("color", "blue");
config.Save(ConfigurationSaveMode.Modified);
string str=WebConfigurationManager.AppSettings["color"];
三。删除
App.config示例:
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
AppSettingsSection appsettings = (AppSettingsSection)config.GetSection("appSettings");
appsettings.Settings.Remove("name");
config.Save();
ConfigurationManager.RefreshSection("name");
string str = ConfigurationManager.AppSettings["name"];
Web.config示例:
Configuration config = WebConfigurationManager.OpenWebConfiguration(null);
AppSettingsSection app = config.AppSettings;
app.Settings.Remove("name");
config.Save();
string str1=WebConfigurationManager.AppSettings["name"];
四。读取
1)App.config示例:
string str=ConfigurationManager.AppSettings["name"];
2)Web.config示例:
string str=WebConfigurationManager.AppSettings["color"];
五。修改
App.config示例:
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings["name"].Value = "李四";
config.Save();
string str = ConfigurationManager.AppSettings["name"];
Web.config示例:
Configuration config = WebConfigurationManager.OpenWebConfiguration(null);
config.AppSettings.Settings["name"].Value = "李四";
config.Save();
string str = ConfigurationManager.AppSettings["name"];
.Net 对App.config和Web.config的访问操作(增、删、读、改)的更多相关文章
- 两种读写配置文件的方案(app.config与web.config通用)
第一种方法:采用MS现有的ConfigurationManager来进行读写 using System.Configuration; namespace Zwj.TEMS.Common { publi ...
- 关于C#和ASP.NET中对App.config和Web.config文件里的[appSettings]和[connectionStrings]节点进行新增、修改、删除和读取相关的操作
最近我做的一些项目,经常需要用到对应用程序的配置文件操作,如app.config和web.config的配置文件,特别是对配置文件中的[appSettings]和[connectionStrings] ...
- App.config和Web.config配置文件的自定义配置节点
前言 昨天修改代码发现了一个问题,由于自己要在WCF服务接口中添加了一个方法,那么在相应调用的地方进行更新服务就可以了,不料意外发生了,竟然无法更新.左查右查终于发现了问题.App.config配置文 ...
- 适用于app.config与web.config的ConfigUtil读写工具类
之前文章:<两种读写配置文件的方案(app.config与web.config通用)>,现在重新整理一个更完善的版本,增加批量读写以及指定配置文件路径,代码如下: using System ...
- Visual Studio中xml文件使用app.config、web.config等的智能提示的方法
在.Net开发的过程中,有时我们需要使用Xml文件作为配置文件(基于某些情况的考虑),而不是app.config.web.config这种,但是我们在xml中配置时希望可以增加类似编辑app.conf ...
- 适用于app.config与web.config的ConfigUtil读写工具类 基于MongoDb官方C#驱动封装MongoDbCsharpHelper类(CRUD类) 基于ASP.NET WEB API实现分布式数据访问中间层(提供对数据库的CRUD) C# 实现AOP 的几种常见方式
适用于app.config与web.config的ConfigUtil读写工具类 之前文章:<两种读写配置文件的方案(app.config与web.config通用)>,现在重新整理一 ...
- 描述 Machine.Config 和 Web.Config(转载)
NET Framework 提供的配置管理包括范围广泛的设置,允许管理员管理 Web 应用程序及其环境.这些设置存储在 XML 配置文件中,其中一些控制计算机范围的设置,另一些控制应用程序特定的配置. ...
- C# 应用程序配置文件App.Config和web.config
应用程序配置文件,对于asp.net是 web.config,对于WINFORM程序是 App.Config(ExeName.exe.config). 配置文件,对于程序本身来说,就是基础和依据,其本 ...
- App.config和Web.config配置文件的配置节点的解析
前言 在http://www.cnblogs.com/aehyok/p/3558661.html这篇博文中,大致对配置文件有了初步的了解,并且在文中有提到过<appSettings>和&l ...
随机推荐
- Web应用程序项目XX已配置为使用IIS
今天在看开源项目Umbraco是,出现一个项目加载不了,并报如下错误: Web应用程序项目Umbraco.Cms.Web.UI已配置为使用IIS.若要访问本地IIS网站,必须安装下列IIS组件..,如 ...
- love easily fade
Ubuntu addict and loving it , the answerer 's intro attracted me.When I first read a book on linux ...
- 在win8.1中安装apache+php+mysql
因为x项目需求,需要学习使用php,咱也没啥经验,win8.1的系统在搭建php的运营环境中,费了一些周折,但是最后还是解决了. 详情请看 原文:http://jingyan.baidu.com/ar ...
- 用R实现全排列的分类
R 其实是个很好用的东东哦-最近写了个小函数,可以实现全排列数的枚举,代码如下: permut<-function(seq){ seq_len=length(seq); if(s ...
- QT中的C/S通信问题:关于服务器端readyread()信号不发射
在windows下用QT测试C/S通信的时候,遇到服务器端不能读取客户端发来的数据的问题. 后来设置断点检查错误,发现是readyread信号不触发的原因. 后来在客户端写socket后面加了一句so ...
- Servlet学习一
Servlet担当着客户请求(Web浏览器或其它HTTP客户程序)与服务器响应(HTTP服务器上数据库或应用程序)的中间层.Servlet是位于Web服务器内部的服务器端Java应用程序,与传统的从命 ...
- 文件类型工具类:FileTypeUtil
个人学习,仅供参考! package com.example.administrator.filemanager.utils;import java.io.File;/** * 文件类型工具类 * * ...
- 顶点着色器详解 (Vertex Shaders)
学习了顶点处理,你就知道固定功能流水线怎么将顶点从模型空间坐标系统转化到屏幕空间坐标系统.虽然固定功能流水线也可以通过设置渲染状态和参数来改变最终输出的结果,但是它的整体功能还是受限.当我们想实现一个 ...
- linux sysnc
rsync -az --delete --exclude="test/exclude/" test /data/sync/ 同步目录文件,排除test/exclude目录 实现目 ...
- Android "adb devices no permissions"
列出当前连接设备时出现以下情况 [user@dell platform-tools]# ./adb devices List of devices attached ???????????? no p ...