3

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?

2
  • Maybe Directory.GetCurrentDirectory() is the bin directory, if you remove SetBasePath method and just add JsonFile it works? Commented Sep 4, 2022 at 23:12
  • Per Gustavo Mauricio De Barros's response above, and your answer below, "Directory.GetCurrentDirectory()" is NOT reliable. Nor is "Environment.CurrentDirectory" (another suggestion below). Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) is the "correct" solution. Thank you for posting your answer. Hopefully SO will let you edit it now :)
    – paulsm4
    Commented Sep 15, 2022 at 0:34

3 Answers 3

7

Ok - found a way this works now. Still looks like a kludge, but it works and I'm happy ... for now.

          var configuration = new ConfigurationBuilder()
         .SetBasePath(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location))     
         .AddJsonFile("appsettings.json")
         .Build();       

Surely, there is a built in function for this, but I can't seem to find an example that works once published.

********** UPDATE *********************

This works perfectly:

        var configuration = new ConfigurationBuilder()
        .SetBasePath(AppContext.BaseDirectory)     
        .AddJsonFile("appsettings.json")
        .Build();
3
  • 2
    No - "Assembly.GetEntryAssembly()" isn't a kludge: it's exactly the correct approach here.
    – paulsm4
    Commented Sep 14, 2022 at 22:34
  • Indeed. I'm posting why CurrentDirectory may or may not work, below (and it should NEVER be assumed to be the same folder as your running application)
    – MC9000
    Commented Sep 14, 2022 at 23:22
  • SO won't let me edit the answer, so see comment below other, incorrect, answer.
    – MC9000
    Commented Sep 14, 2022 at 23:26
3

AppContext.BaseDirectory seems to be a more elegant way to replace Assembly.GetEntryAssembly().Location

You can check out the documentation here

0
0

Environment.CurrentDirectory should work.

You can check the documentation here.

The code snippet looks like this

var configuration = new ConfigurationBuilder()
                       .SetBasePath(Environment.CurrentDirectory)
                       .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
                       .Build();

EDIT: This also works for published apps

EDIT 2: I found a more elegant solution for the scenario described here. Please check my other answer.

4
  • Not on published app, it doesn't. I figure there is a setting in the project that is the problem, but can reproduce it in VS2022. Works in Debug, and Release, just not in Publish (the file is copied over to the Publish folder! - so it's something else).
    – MC9000
    Commented Sep 14, 2022 at 6:07
  • hmmm worked for me using the profile settings in VS2022
    – Leo
    Commented Sep 14, 2022 at 22:20
  • I just shared a screenshot of my configuration above.
    – Leo
    Commented Sep 14, 2022 at 22:26
  • 2
    The reason why CurrentDirectory sometimes works and sometimes does not is dependent on another app the user may be running. It's profile dependent, and if you have another app running and opened a file in it, the CurrentDirectory is NOT for your running app anymore. CurrentDirectory should NEVER be assumed to be the same as the Directory you're running under.
    – MC9000
    Commented Sep 14, 2022 at 23:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.