C# ';配置文件';appsettings.json';未找到,并且不是可选的。物理路径为';C:\Windows\SysWOW64\appsettings.json'';

C# ';配置文件';appsettings.json';未找到,并且不是可选的。物理路径为';C:\Windows\SysWOW64\appsettings.json'';,c#,xaml,.net-core,desktop-application,winui,C#,Xaml,.net Core,Desktop Application,Winui,我正在尝试在我的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'.' 我已经将构建操作设置为content,并将copy设置为output dire

我正在尝试在我的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'.'
我已经将构建操作设置为content,并将copy设置为output directroty以复制较新版本。但什么都不管用

我的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();
            });
}
使用Microsoft.Extensions.Hosting;
使用Microsoft.UI.Xaml;
名称空间管理系统.Web
{
公共部分类应用程序:Microsoft.UI.Xaml.Application
{
公共应用程序()
{
this.InitializeComponent();
}
仅启动受保护的覆盖无效(启动ActivatedEventArgs args)
{
IHost host=CreateHostBuilder(args.Build();
使用(IServiceScope scope=host.Services.CreateScope())
{
IServiceProvider服务=scope.ServiceProvider;
MainWindow MainWindow=services.GetRequiredService();
MainWindow.Activate();
}
}
私有静态IHostBuilder CreateHostBuilder(LaunchActivatedEventArgs)=>
Host.CreateDefaultBuilder()
.ConfigureAppConfiguration((\ux,config)=>
{
config.SetBasePath(Directory.GetCurrentDirectory());
config.AddJsonFile(“appsettings.json”,可选:false,reloadOnChange:true);
})
.ConfigureServices((u,services)=>
{
services.AddOptions();
services.ConfigureServices();
});
}

我不知道为什么会发生这种情况。我试过用谷歌搜索它,但我找不到任何关于为什么会发生这种情况的信息。

经过一番搜索,我找到了解决问题的方法

在将Json文件添加到我的管道之前调用以下代码行修复了这个问题

config.SetBasePath(Package.Current.InstalledLocation.Path);

为什么你的应用程序在那个奇怪的目录中启动?不确定这是我的第一个winUI应用程序