目标:学会读取配置文件中的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); }