【发布时间】:2021-11-10 11:16:05
【问题描述】:
考虑以下 C# 代码:
var json = "{ \"LogLevel\": \"Debug\" }";
const string filePath = @"C:\temp\tempconfiguration.json";
File.WriteAllText(filePath, json);
var jsonConfigurationSource = new JsonConfigurationSource { Path = filePath };
var jsonConfigurationProvider = new JsonConfigurationProvider(jsonConfigurationSource);
var configuration = new ConfigurationRoot(new List<IConfigurationProvider> { jsonConfigurationProvider });
当我运行它时,它会抛出:
System.IO.FileNotFoundException
The configuration file 'C:\temp\tempconfiguration.json' was not found and is not optional.
这对我来说毫无意义。该文件显然存在(我可以在 Windows 资源管理器中看到它)。谁能向我解释这里发生了什么?提前致谢!
(我知道我也可以使用JsonStreamConfigurationProvider 从流中读取,但我希望也能够从文件中读取配置。主要是因为JsonStreamConfigurationProvider 不支持@ 987654325@.)
我正在运行 .NET 5.0 和 C# 9。
【问题讨论】:
标签: c# configuration filenotfoundexception