一、概述

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

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

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

二、配置文件

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="y" value="this is Y"/>
</appSettings>
</configuration>

三、读取配置文件:

1. 读取值:

System.Configuration.ConfigurationManager.AppSettings[“y”];

Asp.Net程序:

System.Web.Configuration.WebConfigurationManager.AppSettings[“y”];

读取最新值:

Configuration config =  ConfigurationManager.OpenExeConfiguration(null);
AppSettingsSection app = config.AppSettings;
// 或者AppSettingsSection app =config.GetSection("AppSettings") as AppSettingsSection

Asp.Net程序读取值:

 Configuration config = WebConfigurationManager.OpenWebConfiguration(null);
AppSettingsSection app = config.AppSettings;
// 或者AppSettingsSection app =config.GetSection("AppSettings") as AppSettingsSection;

2、查看值

string y=app.Settings["y"].Value;
foreach (string key in app.Settings)
{
Console.WriteLine(key+","+ app.Settings[key].Value);
}

3、添加一项

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
AppSettingsSection app = config.AppSettings;
app.Settings.Add("x", "this is X");
config.Save(ConfigurationSaveMode.Modified);

4、修改一项

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

5、删除一项

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,把它理解为是初始化配置文件比较合适。

四、连接字符串

1、读取连接字符串

ConnectionStringSettings conn=   ConfigurationManager.ConnectionStrings["y"];

或者

Configuration config = ConfigurationManager.OpenExeConfiguration(null);
ConnectionStringsSection connSeciton = config.ConnectionStrings;
connSeciton.ConnectionStrings.Add(new ConnectionStringSettings("name", "connectionstring", "provider"));

2、加密字符串

public static void EncryptConnectionString(bool encrypt)
{
Configuration configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
ConnectionStringsSection configSection = configFile.GetSection("connectionStrings") as ConnectionStringsSection;
if ((!(configSection.ElementInformation.IsLocked)) && (!(configSection.SectionInformation.IsLocked)))
{
if (encrypt && !configSection.SectionInformation.IsProtected) //encrypt is false to unencrypt
{
configSection.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");
}
if (!encrypt && configSection.SectionInformation.IsProtected)
{
configSection.SectionInformation.UnprotectSection(); //encrypt is true so encrypt
}
configSection.SectionInformation.ForceSave = true; //re-save the configuration file section
configFile.Save(); // Save the current configuration.
}
}

app.config/web.config配置文件增删改的更多相关文章

  1. winform 配置文件增删改查

    winform 配置文件是  App.config webform   的配置文件 是web.config 其实基本操作都一样    设置个配置文件  全局文件 访问者个配置文件  对这个配置文件增删 ...

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

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

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

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

  4. 【转载】App.config/Web.config 中特殊字符的处理

    写一个网站,遇到一个问题,发布以后,提示错误,但是即使打开错误提示(在web.config中打开),还是只提示错误,没提示什么地方错误,这让我知道了:是webconfig本身的错误,经过排除,是链接字 ...

  5. [转]WinForm和WebForm下读取app.config web.config 中邮件配置的方法

    本文转自:http://blog.csdn.net/jinbinhan/article/details/1598386 1. 在WinForm下读取 App.config中的邮件配置语句如下: Con ...

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

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

  7. 配置文件的继承与覆盖: Machine.config / Web.config /子目录 Web.config

    C#有三种级别的配置文件: 机器级别 machine.config 在 C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Config\machine.c ...

  8. C#读取App.config/Web.config

    读取需要添加 System.Configuration 引用, 两种方式添加: 1:.NETFramework程序可以在引用右击添加引用,然后添加System.Configuration 2:引入Nu ...

  9. applicationhost.config web.config

    在 IIS7 8两个版本中, 用户的配置,可以通过修改如上的配置文件来完成 applicationhost.config ,可以定义全局的 用户自己目录下的web.config,可以自己定义 但是,有 ...

随机推荐

  1. 关于docker的理解随记

    1.容器其实不是什么新技术,说白了就是namespace对资源进行隔离,再加UFS实现分层镜像,以及cgroup实现资源限制.这些技术,都是linux中已有的技术,而且有些技术很早之前就有了. 2.上 ...

  2. SqlHelper---操作数据库

    public class SqlHelper { /// <summary> /// 数据库连接字符串 /// </summary> public static readonl ...

  3. 删除弹出提示框_MVC

    <td> @Ajax.ActionLink(@shared.Delete, "DeleteServicetag", new { id = item.ID }, new ...

  4. 【转】 面向对象(OO)程序设计

    前言 本文主要介绍面向对象(OO)程序设计,以维基百科的解释: 面向对象程序设计(英语:Object-oriented programming,缩写:OOP),指一种程序设计范型,同时也是一种程序开发 ...

  5. SOA(面向服务架构)——踩坑后反思:这样值得吗?

    SOA(面向服务架构)——踩坑后反思:这样值得吗?

  6. OpenGL开发入门

    1.OpenGL简介: OpenGL ES (OpenGL for Embedded Systems) 是 OpenGL三维图形 API 的子集,针对手机.PDA和游戏主机等嵌入式设备而设计.该API ...

  7. 基于Github搭建SrpingCloudConfig详解

    最近在看SpringCloud,为了帮助自己学习和记忆,所以写下这篇文章. 从SpringCloud官方文档上看SpringCloudConfig其实为我们提供配置外部化的一个服务,可以理解成就是个w ...

  8. redis使用及配置之缓存详解

    redis使用及配置之缓存详解 1.Redis的介绍 Redis是一个Key-Value存储系统.它支持存储的value类型有:string(字符串),list(链表), set(无序集合),zset ...

  9. 小工具-IP地址获取和设置及端口访问验证(windows)

    技术部在业务部门眼里就是后勤部门,业务部门要搬到新大楼去 领导要求去帮忙调试业务人员的电脑,要保证这些大爷们周一上班来,就喝着茶打开新浪,然后打开OA看看. 手上就几个桌面支持的兄弟,要弄一百台多电脑 ...

  10. sql:PostgreSQL

    PostgreSQL sql script: -- Database: geovindu -- DROP DATABASE geovindu; CREATE DATABASE geovindu WIT ...