一。首先引用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的访问操作(增、删、读、改)的更多相关文章

  1. 两种读写配置文件的方案(app.config与web.config通用)

    第一种方法:采用MS现有的ConfigurationManager来进行读写 using System.Configuration; namespace Zwj.TEMS.Common { publi ...

  2. 关于C#和ASP.NET中对App.config和Web.config文件里的[appSettings]和[connectionStrings]节点进行新增、修改、删除和读取相关的操作

    最近我做的一些项目,经常需要用到对应用程序的配置文件操作,如app.config和web.config的配置文件,特别是对配置文件中的[appSettings]和[connectionStrings] ...

  3. App.config和Web.config配置文件的自定义配置节点

    前言 昨天修改代码发现了一个问题,由于自己要在WCF服务接口中添加了一个方法,那么在相应调用的地方进行更新服务就可以了,不料意外发生了,竟然无法更新.左查右查终于发现了问题.App.config配置文 ...

  4. 适用于app.config与web.config的ConfigUtil读写工具类

    之前文章:<两种读写配置文件的方案(app.config与web.config通用)>,现在重新整理一个更完善的版本,增加批量读写以及指定配置文件路径,代码如下: using System ...

  5. Visual Studio中xml文件使用app.config、web.config等的智能提示的方法

    在.Net开发的过程中,有时我们需要使用Xml文件作为配置文件(基于某些情况的考虑),而不是app.config.web.config这种,但是我们在xml中配置时希望可以增加类似编辑app.conf ...

  6. 适用于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通用)>,现在重新整理一 ...

  7. 描述 Machine.Config 和 Web.Config(转载)

    NET Framework 提供的配置管理包括范围广泛的设置,允许管理员管理 Web 应用程序及其环境.这些设置存储在 XML 配置文件中,其中一些控制计算机范围的设置,另一些控制应用程序特定的配置. ...

  8. C# 应用程序配置文件App.Config和web.config

    应用程序配置文件,对于asp.net是 web.config,对于WINFORM程序是 App.Config(ExeName.exe.config). 配置文件,对于程序本身来说,就是基础和依据,其本 ...

  9. App.config和Web.config配置文件的配置节点的解析

    前言 在http://www.cnblogs.com/aehyok/p/3558661.html这篇博文中,大致对配置文件有了初步的了解,并且在文中有提到过<appSettings>和&l ...

随机推荐

  1. [jv-convert] Error 1,[all-recursive] Error 1

    g++4.6编译是报错: make profiledbootstrap /var/tmp/gcc4/gcc-4.0.1/sparc-sun-solaris2.8/gcc/gcj -B/var/tmp/ ...

  2. 7.openssl enc

    对称加密工具.了解对称加密的原理后就很简单了. [root@xuexi tmp]# man enc NAME enc - symmetric cipher routines SYNOPSIS open ...

  3. 安装Eclipse及相关Plugin

    Eclipse: 此处选用基于Eclipse的开发工具:STS 官方网站:https://spring.io/tools/sts Plugins: Open Explorer文件浏览插件it open ...

  4. MyEclipse 序列号生成代码

    根据程序运行提示输入用户名即可生成注册码 import java.io.*; public class MyEclipseGen { private static final String LL = ...

  5. 关于 矩阵在ACM中的应用

    关于矩阵在ACM中的应用 1.矩阵运算法则 重点说说矩阵与矩阵的乘法,不说加减法. 支持: 结合律  (AB)C = A(BC) 分配律 A(B+C) = AB + AB $\left( \lambd ...

  6. JavaScript 中this与Dom中的注意

    对于下面这段代码: <script type='text/javascript'> function testThis() { console.log(this); } </scri ...

  7. iOS有用的三方库

    DKNightVersion https://github.com/Draveness/DKNightVersion#podfile 用来为APP添加夜间模式和换肤功能

  8. map创建JSONObject对象

    public static void mapToJSONObject(){ Map<String, Object> map = new HashMap<String,Object&g ...

  9. nullcon HackIM 2016 -- Programming Question 5

    Dont blink your Eyes, you might miss it. But the fatigue and exhaustion rules out any logic, any wil ...

  10. spring mvc 4.3.2 + mybatis 3.4.1 + mysql 5.7.14 +shiro 幼儿园收费系统 之 消息管理