繁体   English   中英

'未找到配置文件'appsettings.json',不是可选的。 物理路径是“C:\Windows\SysWOW64\appsettings.json”。

[英]'The configuration file 'appsettings.json' was not found and is not optional. The physical path is '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'.'

我已将构建操作设置为内容,并将复制到 output 直接复制到更新的副本。 但没有任何效果。

应用设置属性

我的 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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM