[英]Upgraded to VS 2017 15.6.0 and now Directory.GetCurrentDirectory() returns \bin\Debug\netcoreapp2.0
升级后,似乎我所有的 .net 核心控制台应用程序在调试时都得到了错误的路径。
我有以下代码
private static void ConfigureServices(IServiceCollection serviceCollection)
{
// build configuration
var configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", false)
.Build();
}
哪个可以正常工作,但是现在我升级了,出现以下错误
System.IO.FileNotFoundException: 'The configuration file 'appsettings.json' was not found and is not optional. The physical path is 'D:\projects\myproject\bin\Debug\netcoreapp2.0\appsettings.json'.'
我知道我可以将 appsettings.json 复制到这个路径,但我以前从来没有这样做过,而且在升级之前它完全没问题。
我建议完全删除.SetBasePath(Directory.GetCurrentDirectory())
行。 SetBasePath
方法现在是可选的,如果未调用它,那么我们会从应用程序库推断基本路径。
关于你的错误。 dotnet 中有一个修复程序,它改变了行为:
将默认工作目录更改为输出文件夹而不是项目根目录。
看起来你已经通过 VS 更新安装了最新版本的 .NET Core SDK。 所以你现在需要将 appsettings.json 复制到输出文件夹。
将 appsettings.json 文件属性“复制到输出目录”更改为“复制”appsettings.json 文件“复制到输出目录”属性也应设置为“如果更新则复制”,以便应用程序能够访问它.
Startup 从 1.x 到 2.0 发生了一些变化,您不再使用配置生成器,它主要是在幕后完成的,但是您可以在 Program.cs 中声明您的appsettings.json
文件
这是操作方法: https ://learn.microsoft.com/en-us/aspnet/core/migration/1x-to-2x/#add-configuration-providers
试试这个:
string Path = Path.GetFullPath(Path.Combine(Directory.GetCurrentDirectory(), @"..\..\"));
只需从 bin 中向上移动两个目录。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.