.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 ...
随机推荐
- 01HTTP服务&AJAX编程
HTTP服务&AJAX编程 一.服务器 1. 什么是服务器? 能够提供某种服务的机器(计算机)称为服务器. 2.服务器的分类: 1.按系统分类:Lin ...
- AJAX原生JS代码
var http_request = false;function send_request(method,url,content,responseType,callback){ http_reque ...
- Play1+angularjs+bootstrap ++ (idea + livereload)
我的web开发最强组合:Play1+angularjs+bootstrap ++ (idea + livereload) 时间 2012-12-26 20:57:26 Freewind.me原文 ...
- VC++ CArchive及简单的文件操作方法
CArchive 方法用于存取文件 我向你推荐的是使用CArchive,它的使用方法简单且功能十分强大.首先还是用CFile声明一个对象,然后用这个对象的指针做参数声明一个CArchive对象,你就可 ...
- The user specified as a definer ('root'@'%') does not exist
The user specified as a definer ('root'@'%') does not exist 此种报错主要是针对访问视图文件引起的(没有权限) 解决方法: 2.进入mysql ...
- linux服务器加硬盘扩容
from: http://bbs.chinaunix.net/thread-3613556-1-1.html 试验环境: vmware下,centos6,64位版本,原来系统默认分区,/dev/sda ...
- ajax将json写到table中去
查询条件: <table style="width: 100%;border-collapse: collapse;" > <tr> <th styl ...
- myeclipse 控制台打印空指针 ,黏贴控制台sql到plsql有结果集,异常处理
信用公司框架,不够熟悉. 在完成嗲点登录后,写动态页面是遇到,了问题:myeclipse 控制台打印空指针 ,黏贴控制台sql到plsql有结果集,异常处理. 最后大神给看,在接口实现重写的方法里返回 ...
- 绘制相切弧arcTo
绘制相切弧 语法: CanvasRenderingContext2D.arcTo( x1, y1, x2, y2, radius ) 描述: 该方法用于绘制圆弧 绘制的规则是当前位置与第一个参考点连线 ...
- VoLTE 注册流程
1.开关按钮位置: 设置--> 更多--> 移动网络--> 增强型4G LTE模式 2.该设置开关使用了SwitchPreference控件,addEnhanced4GLteSw ...