WYVE

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

目标:学会读取配置文件中的AppSettings

①在App.config中追加AppSettings。AppSettings是键值对,注意定义位置在<configuration></configuration>中

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <appSettings>
    <add key = "name" value = "张三" />
    <add key = "age" value = "10" />
  </appSettings>
</configuration>

 

②在References中追加System.Configuration

③取值

String value = ConfigurationManager.AppSettings["KeyName"];

using System;
using System.Configuration;


public static void Test()
{
    String name = ConfigurationManager.AppSettings["name"];
    int age = Convert.ToInt32(ConfigurationManager.AppSettings["age"]);
    Console.WriteLine("name:"+name);
    Console.WriteLine("age:" + age);
}
 

 

posted on 2018-08-10 11:45  WYVE  阅读(1177)  评论(0编辑  收藏  举报