原文 http://www.cnblogs.com/codealone/archive/2013/09/22/3332607.html

应用程序配置文件,对于asp.net是 web.config,对于WINFORM程序是 App.Config(ExeName.exe.config)。

配 置文件,对于程序本身来说,就是基础和依据,其本质是一个xml文件,对于配置文件的操作,从.NET 2.0 开始,就非常方便了,提供了 System [.Web] .Configuration 这个管理功能的NameSpace,要使用它,需要添加对 System.configuration.dll的引用。

对于WINFORM程序,使用 System.Configuration.ConfigurationManager;

对于ASP.NET 程序, 使用 System.Web.Configuration.WebConfigurationManager;

对于配置文件内容的读取,真是太普遍不过了,如果你的程序里,没有读取配置文件内容的方面,你都不好意思拿出来用

我们以最常见的 AppSettings 小节来作为例子:

假设有如下的配置文件内容:

<?xml version="1.0" encoding="utf-8" ?>

<configuration>

<appSettings>

<add key="y" value="this is Y"/>

</appSettings>

</configuration>

1. 读取值:

  • Asp.Net:   System.Web.Configuration.WebConfigurationManager.AppSettings[“y”];
  • WinForm:  System.Configuration.ConfigurationManager.AppSettings[“y”];

2. 添加一项

  • ASP.NET(需要有写权限):

Configuration config = WebConfigurationManager.OpenWebConfiguration(null);

AppSettingsSection app = config.AppSettings;

app.Settings.Add("x", "this is X");

config.Save(ConfigurationSaveMode.Modified);

  • WinForm:

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

AppSettingsSection app = config.AppSettings;

app.Settings.Add("x", "this is X");

config.Save(ConfigurationSaveMode.Modified);

3. 修改一项

  • Asp.Net

Configuration config = WebConfigurationManager.OpenWebConfiguration(null);

AppSettingsSection app = config.AppSettings;

//app.Settings.Add("x", "this is X");

app.Settings["x"].Value = "this is not Y";

config.Save(ConfigurationSaveMode.Modified);

  • WinForm

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

AppSettingsSection app = config.AppSettings;

//app.Settings.Add("x", "this is X");

app.Settings["x"].Value = "this is not Y";

config.Save(ConfigurationSaveMode.Modified);

4. 删除一项

  • Asp.Net

Configuration config = WebConfigurationManager.OpenWebConfiguration(null);

AppSettingsSection app = config.AppSettings;

app.Settings.Remove("x");

config.Save(ConfigurationSaveMode.Modified);

  • WinForm

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

AppSettingsSection app = config.AppSettings;

app.Settings.Remove("x");

config.Save(ConfigurationSaveMode.Modified);

说明: 需要注意的是,代码所修改的并不是app.config,而是[Application_Name].exe.config这个文件。其中 Application_Name就是你的可执行文件的文件名,而[Application_Name].exe.config才是真正起作用的配置文 件。至于app.config,把它理解为是初始化配置文件比较合适。对于winfom在vs调试下app.config无变化是正常的,bin里面生成 的程序,运行可看到效果。

C#/ASP.NET应用程序配置文件app.config/web.config的增、删、改操作的更多相关文章

  1. C#/ASP.NET应用程序配置文件app.config/web.config的增、删、改操作,无法为请求的 Configuration 对象创建配置文件。

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

  2. asp.net 2.0中新增的web.config的默认namespace功能 (转)

    看上去这个题目比较长,但实际上,我在看资料时发现,这就是说,在asp.net 2.0中,只需要在web.config里定义你要用的那些namespace,则在aspx页面中就不需要再象1.1那样,用 ...

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

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

  4. C# 中的 ConfigurationManager类引用方法应用程序配置文件App.config的写法

    c#添加了Configuration;后,竟然找不到 ConfigurationManager 这个类,后来才发现:虽然引用了using System.Configuration;这个包,但是还是不行 ...

  5. app.config/web.config配置文件增删改

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

  6. 【系统Configmachine.config与自己的应用程序的App.config/Web.Config配置节点重复】解决方法

    自己的应用程序的App.config或Web.Config文件中与系统的C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Configmachine.co ...

  7. 读取、添加、删除、修改配置文件 如(Web.config, App.config)

    private Configuration config; public OperateConfig() : this(HttpContext.Current.Request.ApplicationP ...

  8. ASP.NET程序中动态修改web.config中的设置项目(后台CS代码)

    using System;using System.Collections;using System.ComponentModel;using System.Data;using System.Dra ...

  9. 数据库连接配置 app.config web.config

    通过ADO.Net连接程序和SQLServer数据库的连接字符串: connectionString ="server=(local);database=Demo;integrated se ...

随机推荐

  1. .Net之托管堆资源分配

    托管堆分配资源: 一:进程初始化是,CLR要保留一块联系的地址空间,这个地址空间最初并没有对应的物理存储空间.这个地址空间就是托管堆.托管堆还维护着一个指针,我把它称为NextObjPtr.它指向下个 ...

  2. Magnolia-CMS安装配置

    Magnolia-CMS安装配置 Magnolia-CMS安装配置 介绍:Magnolia 是一个开源基于Java的Web内容管理系统(CMS),构建在Java内容知识库标准(JSR-170).它适合 ...

  3. Java 中使用Jackson反序列化

    Build.gradle: compile group: 'org.codehaus.jackson', name: 'jackson-mapper-lgpl', version: '1.9.13' ...

  4. win8 VS2010 配制OpenGL

    glut下载地址: http://www.opengl.org/resources/libraries/glut/glutdlls37beta.zip glut.h  ---> C:\Progr ...

  5. 如何修改被编译后DLL文件

    原文 http://www.cnblogs.com/wujy/p/3275855.html 我们平时在工作中经常会遇到一些已经被编译后的DLL,而且更加麻烦是没有源代码可以进行修改,只能针对这个DLL ...

  6. web编码(转)

    问题2.浏览器编码方式是根据“响应标头-response header”中的键为“Content-Type”的值来自动选择判断,而不会简单的根据你在html中看到的标签值<meta http-e ...

  7. [Daily] 2014-4-22

    KEEP GOING Think more product when face difference Check value null when insert/remove/update/add ch ...

  8. item Collaborative Filtering

    算法步骤: 1.计算物品相似度 2.根据用户购买记录,推荐相似物品   物品相似度定义: A.    购买i的人里面,有多少比例购买了j    缺点(推荐系统需要能挖掘长尾信息,此处若j很热门,则w趋 ...

  9. dhtmlgrid修改,支持IE10

    因为项目IE升级,导致原来使用的dhtmlgrid无法正常显示,同时通过loadxml接口还有属性不支持. 花了半天时间对dhtmlgrid进行了修改,能够支持IE10正常加载显示. edit by ...

  10. wxpython 中的所有控件及高级应用

    转自http://xoomer.virgilio.it/infinity77/Phoenix/lib.agw.html,,,哈哈终于找到了这块的内容,书上基本没有讲解 This is the Adva ...