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配置文件的讲解的更多相关文章

  1. C# 读写App.config配置文件的方法

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

  2. C#中动态读写App.config配置文件

    转自:http://blog.csdn.net/taoyinzhou/article/details/1906996 app.config 修改后,如果使用cofnigurationManager立即 ...

  3. 关于App.config配置文件

    今天在做复习的时候,突然发现自己无法读取配置文件中的数据库连接字符串,而且检查了半天也没找出原因,最后求助万能的度娘才得以解决—— 1.App.config配置文件在项目中添加后不要修改名称,否则会出 ...

  4. C# 读写App.config配置文件

    一.C#项目中添加App.config配置文件 在控制台程序中,默认会有一个App.config配置文件,如果不小心删除掉,或者其他程序需要配置文件,可以通过添加得到. 添加步骤:右键项目名称,选择“ ...

  5. 【Core】.NET Core中读取App.config配置文件

    1.项目中添加App.config文件 因为.NET Core的项目本质是控制台应用,所以ConfigurationManager的API会去默认读取app.config配置文件,而不是web.con ...

  6. 读写App.config配置文件的方法

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

  7. 修改 App.Config 配置文件 C#

    [转]在WCF程序中动态修改app.config配置文件 今天在个WCF程序中加入了修改配置文件的功能.我是直接通过IO操作修改的app.config文件内容,修改后发现发现其并不生效,用Google ...

  8. 【C#】#103 动态修改App.config配置文件

    对 C/S模式 下的 App.config 配置文件的AppSetting节点,支持配置信息现改现用,并可以持久保存. 一. 先了解一下如何获取 配置信息里面的内容[获取配置信息推荐使用这个] 1.1 ...

  9. C# 读App.config配置文件[2]: .Net Core框架

    C# 读App.config配置文件[1]:.Net Framework框架 C# 读App.config配置文件[2]: .Net Core框架 网上都是.net framework读取配置文件的方 ...

随机推荐

  1. PAT (Advanced Level) 1057. Stack (30)

    树状数组+二分. #include<iostream> #include<cstring> #include<cmath> #include<algorith ...

  2. ural1628 White Streaks

    White Streaks Time limit: 1.0 secondMemory limit: 64 MB The life of every unlucky person has not onl ...

  3. CodeForces 614D Skills

    排序+枚举+二分 最大的那些变成A,小的那部分提高最小值 #include<cstdio> #include<cstring> #include<cmath> #i ...

  4. iOS WebView的用法

    一.UIWebView 可以加载和显示某个URL的网页,也可以显示基于HTML的本地网页或部分网页: a. 加载 URL WebView = [[UIWebView alloc] initWithFr ...

  5. w3c学习总结1

    1.根据 HTML5 规范,在没有其他合适标签更合适时,才应该把 <b> 标签作为最后的选项.HTML5 规范声明:应该使用 <h1> - <h6> 来表示标题,使 ...

  6. Cookie mapping技术

    摘要: RTB竞价中的cookie mapping技术解决DSP的cookie跟ad change的cookie匹配问题. Cookie mapping分为两步:(1)google ad exchan ...

  7. HUST 1354 Rubiks

    背包.注释写详细了. 本想这样写:每个组内各自做背包,然后组间做背包,但是由于这题M=10000,时间复杂度太大. #include<cstdio> #include<cstring ...

  8. phpcms的网页替换

    //替换首页header:loge里面的首页不用替换<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ...

  9. Delphi 常用函数(数学函数)round、trunc、ceil和floor

    源:Delphi 常用函数(数学函数)round.trunc.ceil和floor Delphi 常用函数(数学) Delphi中怎么将实数取整? floor 和 ceil 是 math unit 里 ...

  10. Maven的安装环境配置

    一.Maven的安装 二.Maven的配置 Settings.xml可以用来定义本地仓库.远程仓库.联网代理 Settings.xml文件可以存在两个地方: 1.多用户情况 conf目录下 2.单用户 ...