C# App.config配置文件的讲解
App.config是C#开发WinForm程序的配置文件,开发Web程序的配置文件叫Web.config。本文介绍App.config的简介使用。
我们先来打开一个App.config文件,看看它的内容像什么样子。
|
1 2 3 4 5 6 7 8 9 10 |
<?xml version="1.0" encoding="utf-8" ?> <configuration> <connectionStrings> <add name="conn" connectionString="this is connection string"/> </connectionStrings> <appSettings> <add key="key1" value="value1" /> <add key="key2" value="value2" /> </appSettings> </configuration> |
从这段代码可以看出,App.config完全是xml文档。它有如下几个特点。
1、它有一个<configuration>标签,所有的配置项都在<configuration>标签下面。
2、C#内置了一个<connectionStrings>节点,专门用于配置数据库连接字符串。它下面可以使用<Add>节点来添加多个数据库连接字符串。
3、<appSettings>,大家了解Web.config的应该很熟悉它,它可以配置任何key-value这样的键值对。最早没有<connectionStrings>的时候,我们也是把数据库连接的字符串放在<appSettings>里面的。
下面我们写一个程序来看看怎么从App.config这个程序配置文件中获取配置信息。
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
using System; using System.Collections.Generic; using System.Collections; using System.Linq; using System.Text; using System.Configuration; namespace AppConfig { class Program { static void Main(string[] args) { Console.WriteLine("ConnectionStrings:"); // ConfigurationManager.ConnectionStrings是一个ConnectionStringSettingsCollection对象 // 按数字循环得到一个个ConnectionStringSettings对象 // 每个ConnectionStringSettings对象有Name和ConnectionString属性 for (int i = 0; i < ConfigurationManager.ConnectionStrings.Count; i++) { string name = ConfigurationManager.ConnectionStrings[i].Name; string connectionString = ConfigurationManager.ConnectionStrings[i].ConnectionString; Console.WriteLine(i.ToString() + ". " + name + " = " + connectionString); } //也可以如下操作,使用ConnectionStringSettings类型来进行foreach遍历 foreach (ConnectionStringSettings conn in ConfigurationManager.ConnectionStrings) { string name = conn.Name; string connectionString = conn.ConnectionString; Console.WriteLine(name + " = " + connectionString); } //直接获取conn的值 Console.WriteLine("\r\nGet the value of the node named \"conn\":"); Console.WriteLine(ConfigurationManager.ConnectionStrings["conn"].ConnectionString); Console.WriteLine(""); Console.WriteLine("AppSettings:"); //AppSettings是NameValueConnection类型,使用AllKeys返回一个所有Key组成的字符串数组 string[] keys = ConfigurationManager.AppSettings.AllKeys; for (int i = 0; i < keys.Length; i++) { string key = keys[i]; //通过Key来索引Value string value = ConfigurationManager.AppSettings[key]; Console.WriteLine(i.ToString() + ". " + key + " = " + value); } // 没有NameValuePair这样的对象,所以无法使用foreach来进行循环 //直接获取key1的值 Console.WriteLine("\r\nGet the value of the key named \"key1\":"); Console.WriteLine(ConfigurationManager.AppSettings["key1"]); //pause the process Console.ReadKey(); } } } |
上面演示了如何遍历<connectionStrings>,如何遍历<appSettings>,如何单独获取某一个connectionString,如何单独获取某个appSetting。大家可以收藏一下,以便以后使用时参考。
App.config和Web.config的语法格式是完全一样的,只是一个用户WinForm程序,一个用于Web程序。
App.config在编译之后,将会被编译到bin\Debug目录下,文件名为“应用程序名.exe.config”。
关于C#中App.config文件的使用,本文就介绍这么多,希望对您有所帮助,谢谢!
C# App.config配置文件的讲解的更多相关文章
- C# 读写App.config配置文件的方法
我们经常会希望在程序中写入一些配置信息,例如版本号,以及数据库的连接字符串等.你可能知道在WinForm应用程序中可以利用Properties.Settings来进行类似的工作,但这些其实都利用了Ap ...
- C#中动态读写App.config配置文件
转自:http://blog.csdn.net/taoyinzhou/article/details/1906996 app.config 修改后,如果使用cofnigurationManager立即 ...
- 关于App.config配置文件
今天在做复习的时候,突然发现自己无法读取配置文件中的数据库连接字符串,而且检查了半天也没找出原因,最后求助万能的度娘才得以解决—— 1.App.config配置文件在项目中添加后不要修改名称,否则会出 ...
- C# 读写App.config配置文件
一.C#项目中添加App.config配置文件 在控制台程序中,默认会有一个App.config配置文件,如果不小心删除掉,或者其他程序需要配置文件,可以通过添加得到. 添加步骤:右键项目名称,选择“ ...
- 【Core】.NET Core中读取App.config配置文件
1.项目中添加App.config文件 因为.NET Core的项目本质是控制台应用,所以ConfigurationManager的API会去默认读取app.config配置文件,而不是web.con ...
- 读写App.config配置文件的方法
我们经常会希望在程序中写入一些配置信息,例如版本号,以及数据库的连接字符串等.你可能知道在WinForm应用程序中可以利用Properties.Settings来进行类似的工作,但这些其实都利用了Ap ...
- 修改 App.Config 配置文件 C#
[转]在WCF程序中动态修改app.config配置文件 今天在个WCF程序中加入了修改配置文件的功能.我是直接通过IO操作修改的app.config文件内容,修改后发现发现其并不生效,用Google ...
- 【C#】#103 动态修改App.config配置文件
对 C/S模式 下的 App.config 配置文件的AppSetting节点,支持配置信息现改现用,并可以持久保存. 一. 先了解一下如何获取 配置信息里面的内容[获取配置信息推荐使用这个] 1.1 ...
- C# 读App.config配置文件[2]: .Net Core框架
C# 读App.config配置文件[1]:.Net Framework框架 C# 读App.config配置文件[2]: .Net Core框架 网上都是.net framework读取配置文件的方 ...
随机推荐
- ecshop二次开发添加快递
以天天快递为例:步骤1.打开includes\modules\shipping文件夹,把sto_express.php复制多一份,重名为tt_express.php:步骤2.打开tt_express. ...
- Spring MVC 与ExtJS完美集成
http://blog.csdn.net/q262800095/article/details/12021191 http://www.jb51.net/article/25267.htm
- springMVC注解及优化
1. 新建web project 2. 添加jar 3. 改写web.xml, 注意spring-servlet.xml的名字 <?xml version="1.0" enc ...
- POJ3352 Road Construction 双连通分量+缩点
Road Construction Description It's almost summer time, and that means that it's almost summer constr ...
- maven bundle
今天引入了几个bundle到pom,尽然说missing,我还以为是nexus组织下载.把type=bundle去掉可以下载,后来同事给了我这个连接https://issues.apache.org/ ...
- SpringMVC接收页面表单参数-java-电脑编程网
body { font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI ...
- STM32实现HID和u盘复合设备
USB设备可以定义一个复合设备,复合设备分两种,一种是一个设备多个配置,还有一种是一个配置多个接口,在本例中采用一个配置多个接口的方式 首先修改设备描述符,标准设备描述符和报告描述符都不需要修改, ...
- LPC1768的看门狗定时器使用
void wwdg_init(void) { LPC_SC->PCLKSEL0 |= (3<<0);//分频数为八分频 LPC_WDT->WDCLKSEL &= ~(3 ...
- iOS开发——An App ID with identifier "*****" is not avaliable
Error: An App ID with identifier "*****" is not avaliable. Please enter a different string ...
- Unity3D ——强大的跨平台3D游戏开发工具(二)
第二章 Unity3D的简单预览 每个Unity3D版本都会自带一个Demo源文件.在3.0的正式版中,自带的Demo就是网上展示的那款强大的射击游戏.在一般情况下,您只要第一次 打开Unity3D ...