您的位置:首页 > 编程语言 > C#

C#如何在控制台应用程序中加入配置文件

2013-10-30 11:46 246 查看

C#如何在控制台应用程序中加入配置文件

一、在控制台程序中加入xml文档命名为App.config(记住一定要起这个名字,否则不能访问)文件的内容如下:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="key0" value="0"></add>
<add key="key1" value="1"></add>
<add key="key2" value="2"></add>
</appSettings>
</configuration>

二、控制台程序中使用配置文件中的值

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;//添加System.Configuration.dll的引用
using System.Collections.Specialized;
namespace test
{
//注意一定要记住配置文件的名称为App.config
class Program
{
static void Main(string[] args)
{
string str = ConfigurationManager.AppSettings.Get("key0");
Console.WriteLine(str);
NameValueCollection sall = ConfigurationManager.AppSettings;
foreach (string s in sall.Keys)
{
Console.WriteLine(s+":"+sall.Get(s));
}
Console.ReadKey();
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: