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

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();
        }
    }
}
posted @ 2013-10-30 11:46  拾破烂的  阅读(3544)  评论(0编辑  收藏  举报