我正在尝试在我的 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();
});
}
我不知道为什么会这样。我试过用谷歌搜索它,但我找不到任何关于为什么会发生这种情况的信息。