Home > Software engineering >  Problem with configuration in .NET,problem with read a json file(I'm using the latest version o
Problem with configuration in .NET,problem with read a json file(I'm using the latest version o

Time:05-05

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'.'

Also, I tried to set the base path, but it didn't help, It can be supposed with the Android system security. Screenshots:

https://i.stack.imgur.com/Nec8J.png
https://i.stack.imgur.com/ttNLg.png
https://i.stack.imgur.com/G6EEl.png
https://i.stack.imgur.com/zcp4d.png
https://i.stack.imgur.com/biTNJ.png
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:

Using it as the Microsoft.Extensions.Configuration method won't work for the time being, it's being considered, but at the earliest for .NET 7: https://github.com/dotnet/maui/issues/4408

If you want to read a file from your app bundle, do the following:

  1. Add the file to your Resources\Raw folder
  2. Make sure the build action is set to MauiAsset
  3. Read the file contents like below and do with it as you please :)
using var stream = await FileSystem.OpenAppPackageFileAsync("foo.json");
using var reader = new StreamReader(stream);
var fileContent = reader.ReadToEnd();

// For example deserialize JSON here.
  • Related