c# - '未找到配置文件'appsettings.json',不是可选的。物理路径是“C:\Windows\SysWOW64\appsettings.json”。
问题描述
我正在尝试在我的 WinUI 桌面应用程序中实现配置文件。我不断收到以下错误。
System.IO.FileNotFoundException: 'The configuration file 'appsettings.json' was not found and is not optional. The physical path is 'C:\Windows\SysWOW64\appsettings.json'.'
我已将构建操作设置为内容,并将副本设置为将直接输出到更新的副本。但没有任何效果。
我的 App.xaml.cs 代码是。
using Microsoft.Extensions.Hosting;
using Microsoft.UI.Xaml;
namespace ManagementSystem.Web
{
public partial class App : Microsoft.UI.Xaml.Application
{
public App()
{
this.InitializeComponent();
}
protected override void OnLaunched(LaunchActivatedEventArgs args)
{
IHost host = CreateHostBuilder(args).Build();
using (IServiceScope scope = host.Services.CreateScope())
{
IServiceProvider services = scope.ServiceProvider;
MainWindow MainWindow = services.GetRequiredService<MainWindow>();
MainWindow.Activate();
}
}
private static IHostBuilder CreateHostBuilder(LaunchActivatedEventArgs args) =>
Host.CreateDefaultBuilder()
.ConfigureAppConfiguration((_, config) =>
{
config.SetBasePath(Directory.GetCurrentDirectory());
config.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
})
.ConfigureServices((_, services) =>
{
services.AddOptions();
services.ConfigureServices();
});
}
我不知道为什么会这样。我试过用谷歌搜索它,但我找不到任何关于为什么会发生这种情况的信息。
解决方案
经过一番搜索,我找到了解决问题的方法。
在将 Json 文件添加到我的管道之前调用以下代码行修复了该问题。
config.SetBasePath(Package.Current.InstalledLocation.Path);
推荐阅读
- scala - Scala 3 - 在一阶类型上提取包装器和 InverseMap 元组
- swift - 是否存在 firstIndex 在 Swift 中不返回可选项的情况?
- python - 运行 spark-submit 时没有模块错误
- azure - Azure API 管理 - 如何使用 Azure Cli 添加 API 和操作?
- microsoft-graph-api - 向 MS Graph API 发出批处理请求时出现未经授权的错误 401
- python - 如何在不使用 Django 中的 models.py 的情况下连接到外部 MySQL
- automation - Ansible cisco ios,关闭未连接的接口
- python - 如何克服使用 pip 安装库时的错误
- javascript - 列出 Google Photos API 中的所有“人物”相册
- java - 方法引用 lambda 中的显式类型提示导致原始类型