在控制台程序中,添加config文件

一、右击类库 → 添加 → 新建项 → 应用程序配置文件(或者选择一个XML文件,然后将名字改成XXX.config),内容如下:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="constr" value="0"></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
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = System.Configuration.ConfigurationManager.AppSettings.Get("constr");
            Console.WriteLine(str);
            NameValueCollection sall = ConfigurationManager.AppSettings;
            foreach (string s in sall.Keys)
            {
                Console.WriteLine(s+":"+sall.Get(s));
            }
            Console.ReadKey();
        }
    }
}
原文地址:https://www.cnblogs.com/vichin/p/7116643.html