skip to Main Content

any hints as to why this throws a "configuration file was not found and is not optional" exception?

string relative_path = "../../../../appsettings_global.json";
if (File.Exists(relative_path)) {
   IConfigurationRoot global_config = new ConfigurationBuilder().AddJsonFile(relative_path, false, true).Build();
   global_settings = global_config.GetRequiredSection("Settings").Get<GLOBAL_SETTINGS>();
}

2

Answers


  1. Chosen as BEST ANSWER

    i found that the following will do what i need; i.e., convert the relative path to an absolute. if there are other/better answers, please do post them.

    string absolute_path = System.IO.Path.GetFullPath("../../../../appsettings_global.json");
    IConfigurationRoot global_config = new ConfigurationBuilder().AddJsonFile(absolute_path, false, true).Build();
    global_settings = global_config.GetRequiredSection("Settings").Get<GLOBAL_SETTINGS>();
    

  2. picture1

    according to the picture1 the file path should be "test/appsettings_global.json"

    And make sure appsettings_global.json file is selected "Copy if newer" or "Copy always" under properties > Copy to Output Directory of the file

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search