Trying to convert Dotnet Core 3.1 to Dotnet6 CONSOLE applications and keep getting
"The configuration file 'appsettings.json' was not found and is not optional. The expected physical path was 'C:\Windows\system32\appsettings.json'."
error message. The thing is, the appsettings.json file IS copied to the publish folder, but I get the error in the Application log and nothing happens.
There are other posts here, but I can't find one specific to DotNet6 (they got rid of startup.cs and are doing things radically different) AND specific to console applications.
I have the appsettings.json Build Action set to Content (or nothing) and set to Copy To Output Directory to Copy Always. The file is absolutely present in the publish folder (and on the server it's running on).
static async Task Main(string[] args)
{
var configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory()) //doesn't work on publish (appsettings.json set to "copy always" - no joy) - thing is that is DOES copy it over, but the program can't find it!
.AddJsonFile("appsettings.json")
.Build();
I'm presuming the .SetBasePath part is incorrect as in other solutions here, but can't apply dotnet5 solutions to this problem. I know I'm doing something wrong, but can't find a solution for DotNet6 console app configuration. Some suggest to create the project in DotNet5, then convert to DotNet6, but that really doesn't fix the problem (too hacky) nor do I learn the new/correct way to configure it. Any ideas?
Path.GetDirectoryName(Assembly.GetEntryAssembly().Location)
is the "correct" solution. Thank you for posting your answer. Hopefully SO will let you edit it now :)