app.config/web.config配置文件增删改
一、概述
应用程序配置文件,对于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配置文件增删改的更多相关文章
- winform 配置文件增删改查
winform 配置文件是 App.config webform 的配置文件 是web.config 其实基本操作都一样 设置个配置文件 全局文件 访问者个配置文件 对这个配置文件增删 ...
- C#/ASP.NET应用程序配置文件app.config/web.config的增、删、改操作
原文 http://www.cnblogs.com/codealone/archive/2013/09/22/3332607.html 应用程序配置文件,对于asp.net是 web.config,对 ...
- C#/ASP.NET应用程序配置文件app.config/web.config的增、删、改操作,无法为请求的 Configuration 对象创建配置文件。
应用程序配置文件,对于asp.net是 web.config,对于WINFORM程序是 App.Config(ExeName.exe.config). 配置文件,对于程序本身来说,就是基础和依据,其本 ...
- 【转载】App.config/Web.config 中特殊字符的处理
写一个网站,遇到一个问题,发布以后,提示错误,但是即使打开错误提示(在web.config中打开),还是只提示错误,没提示什么地方错误,这让我知道了:是webconfig本身的错误,经过排除,是链接字 ...
- [转]WinForm和WebForm下读取app.config web.config 中邮件配置的方法
本文转自:http://blog.csdn.net/jinbinhan/article/details/1598386 1. 在WinForm下读取 App.config中的邮件配置语句如下: Con ...
- 【系统Configmachine.config与自己的应用程序的App.config/Web.Config配置节点重复】解决方法
自己的应用程序的App.config或Web.Config文件中与系统的C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Configmachine.co ...
- 配置文件的继承与覆盖: Machine.config / Web.config /子目录 Web.config
C#有三种级别的配置文件: 机器级别 machine.config 在 C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Config\machine.c ...
- C#读取App.config/Web.config
读取需要添加 System.Configuration 引用, 两种方式添加: 1:.NETFramework程序可以在引用右击添加引用,然后添加System.Configuration 2:引入Nu ...
- applicationhost.config web.config
在 IIS7 8两个版本中, 用户的配置,可以通过修改如上的配置文件来完成 applicationhost.config ,可以定义全局的 用户自己目录下的web.config,可以自己定义 但是,有 ...
随机推荐
- Python(3):文件读写与异常
访问路径: 文件读写必然涉及到文件会放在某个路径下.在python里,可以通过引入os包来实现切换当前访问的路径: # 假设我在 /home/zyq/KiDe/Python/test 文件夹中有一个文 ...
- 屏幕置顶(WindowManager服务)
https://www.cnblogs.com/mythou/p/3244208.html
- Cheatsheet: 2018 05.01 ~ 07.31
JAVA Java Tips: Creating a Monitoring-Friendly ExecutorService Other Modeling the Card Game War in C ...
- 七、curator recipes之阻塞队列SimpleDistributedQueue
简介 Java在单机环境实现了BlockQueue阻塞队列,与之类似的curator实现了分布式场景下的阻塞队列,SimpleDistributedQueue 官方文档:http://curator. ...
- File类的常用方法和练习
File类常用的判断方法 import java.io.File; import java.text.SimpleDateFormat; import java.util.Date; /** * Fi ...
- 讨论!MyBatis中利用package自动扫描包中的类,默认别名不只是首字母小写!
问题描述:这个问题我是在看书的时候碰到的.书上写着通过package标签扫描包中的类,将其第一个字母变为小写作为其别名.我在网上查了一些博主也是这么写的 但是!我发现,无论大小写,只要是类名就好,而且 ...
- #if, #elif, #else, #endif 使用
程序想要通过简单地设置一些参数就生成一个不同的软件,在不同的情况下可能只用到一部分代码,就没必要把所有的代码都写进去,就可以用条件编译,通过预编译指令设置编译条件,在不同的需要时编译不同的代码. (一 ...
- linux环境下mysql 5.7.1X 如何重置root密码
1,vi /etc/my.cnf [mysqld]下加入参数skip-grant-tables 保存退出. 2,重启mysql [root@21yunwei src]# /etc/init.d/ ...
- Spring入门(二)— IOC注解、Spring测试、AOP入门
一.Spring整合Servlet背后的细节 1. 为什么要在web.xml中配置listener <listener> <listener-class>org.springf ...
- CSS,js,html
图片盗链问题使用以下meta标签解决 <meta name="referrer" content="never"> Chrome 中文界面下默认会将 ...