Home > Mobile >  Problem with configuration in .NET,problem with read a json file
Problem with configuration in .NET,problem with read a json file

Time:04-30

When I'm trying to add the JSON file via.AddJsonFile(), it throws a:

System.IO.FileNotFoundException: 'The configuration file 'appsettings.json' was not found and is not optional. The expected physical path was '/data/user/0/com.companyname.rakeshproj/files/appsettings.json'.'

Screenshots:

enter image description here

enter image description here

enter image description here

namespace RakeshProj;
using Microsoft.Extensions.Configuration;
public partial class AppShell : Shell
{
    public AppShell()
    {
        InitializeComponent();
        //Extensions.JsonRead(@"C:\Users\Matsenko\source\repos\TestJson\TestJson\appsettings.json");
        /////////////////Problem
        IConfiguration config = new ConfigurationBuilder()
        //.AddJsonFile("appsettings`.json", optional: true, reloadOnChange: true)
        .AddJsonFile("appsettings.json")
        .AddEnvironmentVariables()
        .Build();
        /////////////////Problem
        //Menu menu_settings = config.GetSection("ShellContentNames").Get<Menu>();


        ShellContent[][] ShellContentName= 
        {
        new ShellContent[3]
        {
            ShellContent1_1,ShellContent2_1,ShellContent3_1
        },
        new ShellContent[13]
        {
                 ShellContent1_2,ShellContent2_2,ShellContent3_2,
                 ShellContent4_2,ShellContent5_2,ShellContent6_2,
                 ShellContent7_2,ShellContent8_2,ShellContent9_2,
                 ShellContent10_2,ShellContent11_2,ShellContent12_2,ShellContent13_2    
        }
        };
        string[][] ShellContentTitle =
        {
        new string[3]
        {
            "Test1_1","Test2_1","Test3_1"
        },
        new string[13]
        {
                 "Test1_2","Test2_2","Test3_2",
                 "Test4_2","Test5_2","Test6_2",
                 "Test7_2","Test8_2","Test9_2",
                 "Test10_2","Test11_2","Test12_2","Test13_2"
        }
        };

        for (int i = 0; i < ShellContentName.Length; i  )
        {
            for(int j=0; j < ShellContentName[i].Length; j  )
            {
                ShellContentName[i][j].Title = ShellContentTitle[i][j];
                SemanticScreenReader.Announce(ShellContentName[i][j].Title);
            }
        }

    }
}

CodePudding user response:

I wonder if this has something to do with your configuration builder not knowing where that file is being Copy Always-ed to. I like to be explicit by setting the base path of my config builder to the location of the executing or entry point assembly.

Try one of these extensions off the builder:

  • Using enter image description here

    Build action: Content

    Copy to output directory: Copy if newer

  • Related