c# - How to read values from config.json in Console Application -
i installed asp.net 5 , created console application in visual studio. i've added file, config.json, root folder of project.
it looks this:
{ "data": { "targetfolderlocations": { "testfolder1": "some path", "testfolder2": "another path" } }
}
my program.cs looks this
public void main(string[] args) { var configurationbuilder = new configurationbuilder(environment.currentdirectory) .addjsonfile("config.json") .addenvironmentvariables(); configuration = configurationbuilder.build(); //doesn't work...null time var test = configuration.get("data:targetfolderlocations"); console.readline(); }
how can access targetfolderlocations key code?
have type following:
public class foldersettings { public dictionary<string, string> targetfolderlocations { get; set; } }
you can use configurationbinder
automatically bind configuration sections types above. example:
var foldersettings = configurationbinder.bind<foldersettings>(config.getconfigurationsection("data")); var path = foldersettings.targetfolderlocations["testfolder1"];
Comments
Post a Comment