一、概述

应用程序配置文件,对于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. <机器学习实战>读书笔记--logistic回归

    1. 利用logistic回归进行分类的主要思想是:根据现有数据对分类边界线建立回归公式,以此进行分类. 2.sigmoid函数的分类 Sigmoid函数公式定义 3.梯度上升法    基本思想:要找 ...

  2. centos7中安装mongodb3.6

    centos7中安装mongodb3.6 首先更新系统 yum -y update 1.安装Mongodb 编辑Mongodb安装源 vim /etc/yum.repos.d/mongodb-org- ...

  3. 关于cmder 目录右键打开当前目录的方法

    在 Cmder 目录直接运行 cmder /register user或者cmder /register all

  4. Jsp&Servlet入门级项目全程实录第1讲

    惯例广告一发,对于初学真,真的很有用www.java1234.com,去试试吧! 1.jdbc数据库连接(略) 2.登录表单 2.1设置内边距 <dir style="padding- ...

  5. java 基础 --- servlet

    问题 : servlet 这个类是有什么作用 概述 servlet 是个接口,这个接口的作用是规范了接收请求的处理类.而最终的实现交给了 servlet 容器去实现. servlet 接口 接口方法如 ...

  6. [编程] C语言循环结构计算π的值

    公式: 分析:首先,系数为正数的项的分母是4n-3(n为正数项的项数),为负数的项的分母为4n-1(n为负数项的项数),即分母的变化规律是1.3.5.7...的奇数数列,则第n项的分母为2n-1,第1 ...

  7. Java8增强的Map集合

    Map集合简介 Map用于保存具有映射关系的数据,因此Map集合里保存着两组值,一组值用于保存Map里的key,另外一组用于保存Map里的vlaue,key和value都可以是任何引用类型的数据. M ...

  8. java使用lock实现一个简单的死锁程序

    import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.ReentrantLock; public ...

  9. SSM迁移到Springboot记录

    日志问题 Exception in thread "main" java.lang.IllegalArgumentException: LoggerFactory is not a ...

  10. python可变容器类型做函数参数的坑

    def extendList(val, list=[]): # []默认参数的只指向一个地址 list.append(val) return list list1 = extendList(10) l ...