1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| //用来构建数据源 IConfigurationBuilder builder = new ConfigurationBuilder(); //定义了一个内存的数据配置源 builder.AddInMemoryCollection(new Dictionary<string, string> { {"key1", "value1" }, {"key2", "value2" }, {"section1:key3", "value3" }, {"section2:section3:key4", "value4" } }); //构建数据源 IConfigurationRoot configurationRoot = builder.Build(); //读取配置信息 Console.WriteLine($"key1:{configurationRoot["key1"]}"); Console.WriteLine($"key2:{configurationRoot["key2"]}"); //定义一个节点,用来读取value3 IConfigurationSection section1 = configurationRoot.GetSection("section1"); Console.WriteLine($"section1:key3:{section1["key3"]}"); //嵌套节点 IConfigurationSection section2 = configurationRoot.GetSection("section2"); var section3 = section2.GetSection("section3"); Console.WriteLine($"section2:section3:key4:{section3["key4"]}");
|