如何解决'找不到配置文件'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);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。