在运行时检索 appSettings.json

问题描述

有没有办法检索 appSettings.json 的路径以便将它们加载到 FileSystemWatcher 实例中?

解决方法

我找到了解决方案! 对于所有好奇的人,我已经解决了以下问题:

string value;
IConfigurationRoot config = new ConfigurationBuilder().AddJsonFile("appsettings.json",false,true).Build();
IConfigurationProvider provider = config.Providers.ToList().First(); //There could be several providers but in my case I have only one
this.provider.Load();//reloads the content of the appSettings.json
this.provider.TryGet("section:subProperty",out value);

如果有人有更好的方法,请发布。我会很高兴阅读它。 :)