应用程序配置文件,对于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);

ConfigurationManager.RefreshSection("appSettings");// 刷新命名节,在下次检索它时将从磁盘重新读取它。记住应用程序要刷新节点

【修改后,App.config文件的x节点没有更改,而是exe.config的配置更改,读取正常】

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);

C# 应用程序配置文件操作的更多相关文章

  1. .NET程序配置文件操作(ini,cfg,config)

    在程序开发过程中,我们一般会用到配置文件来设定一些参数.常见的配置文件格式为 ini, xml, config等. INI .ini文件,通常为初始化文件,是用来存储程序配置信息的文本文件. [Log ...

  2. Config程序配置文件操作实践进阶之ConfigurationSectionGroup

    今天又进一步对System.Configuration下的ConfigurationSectionGroup类及相关的类与方法进行了研究.发现要构建多层次嵌套的XML标签 则必须用到Configura ...

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

    原文 http://www.cnblogs.com/codealone/archive/2013/09/22/3332607.html 应用程序配置文件,对于asp.net是 web.config,对 ...

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

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

  5. Config程序配置文件(configSections)操作实践及代码详注

    所有与配置文件相关的类:(粗体为一般情况下使用到的类,其它类功能可能在很复杂的情况下才使用到.) 1.ConfigurationManager,这个提供用于打开客户端应用程序集的Configurati ...

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

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

  7. C#应用程序配置文件.config介绍

    我们经常会希望在程序中写入一些配置信息,例如版本号,以及数据库的连接字符串等.你可能知道在WinForm应用程序中可以利用Properties.Settings来进行类似的工作,但这些其实都利用了Ap ...

  8. .NET 多个程序配置文件合并到主app.config

    .NET 多个程序配置文件合并到主app.config

  9. 配置文件操作(ini、cfg、xml、config等格式)

    配置文件的格式主要有ini.xml.config等,现在对这些格式的配置文件的操作(C#)进行简单说明. INI配置文件操作 调用系统函数GetPrivateProfileString()和Write ...

随机推荐

  1. swift GCD使用指南

    swift GCD使用指南 Grand Central Dispatch(GCD)是异步执行任务的技术之一.一般将应用程序中记述的线程管理用的代码在系统级中实现.开发者只需要定义想执行的任务并追加到适 ...

  2. [Android]Android Debug key 的制作

    Android Debug key 的制作 背景 在Android App 开发过程中,我们经常会使用一些第三方的服务,但是很多的第三方服务都会要求我们提供包名,签名安装包,这时候,我们在日常调试时, ...

  3. iOS实现(超级猜图)源码

    //首先建立模型文件 QLLQuestion.hheQLLQuestion.m文件 #import <Foundation/Foundation.h> @interface QLLQues ...

  4. [android] 手机卫士界面切换动画

    在/res/anim/ 建立文件tran_out.xml 添加<translate>节点 设置x轴来源坐标android:fromXDelta=”0” 设置x轴目的坐标android:to ...

  5. 保持listview当前位置

    保持listview滑动的位置,一般用在增加listview子item中布局的评论或者退出当前活动,再次进入继续阅读时. 利用ListView.getFirstVisiblePosition()来获取 ...

  6. Mac平台下启动MySQL到完全终止MySQL----终端八步走

    1.选中Finder的情况下,快捷键进入搜索目录:/usr/local 然后进入mysql目录下: 2.右键 "从这里启动" 打开终端: 3.输入执行:./scripts/mysq ...

  7. Memcache笔记05-Memcache安全性

    Memcache服务器端都是直接通过客户端连接后直接操作,没有任何的验证过程,这样如果服务器是直接暴露在互联网上的话是比较危险,轻则数据泄露被其他无关人员查看,重则服务器被入侵,因为Mecache是以 ...

  8. 在IE8等不支持placeholder属性的浏览器中模拟placeholder效果

    placeholder是一个很有用的属性,可以提示用户在input框中输入正确的内容,但是IE8以及IE8一下的浏览器不支持该属性,我们可以使用js来模拟相似的效果.下面直接上代码: <!doc ...

  9. netty-socketio

    一.简介 netty-socketio是一个开源的Socket.io服务器端的一个java的实现,它基于Netty框架.项目地址为:https://github.com/mrniko/netty-so ...

  10. internet connection sharing has been disabled by the network administrator

    Start > Run > gpedit.msc Locate; Computer Configuration/Administrative Templates/Network/Netwo ...