.NET 中的配置问题,读取 json 文件时出现问题(我使用的是最新版本的移动开发框架)

Problem with configuration in .NET,problem with read a json file(I'm using the latest version of Mobile devopment framework)

当我尝试添加 JSON 文件 via.AddJsonFile() 时,它抛出一个:

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

此外,我尝试设置基本路径,但没有帮助,可以推测 Android 系统安全。 截图:

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);
            }
        }

    }
}

作为Microsoft.Extensions.Configuration方法暂时不行,正在考虑中,但最早用于.NET 7:https://github.com/dotnet/maui/issues/4408

如果您想从应用程序包中读取文件,请执行以下操作:

  1. 将文件添加到您的 Resources\Raw 文件夹
  2. 确保构建操作设置为 MauiAsset
  3. 阅读下面的文件内容并根据需要进行操作:)
using var stream = await FileSystem.OpenAppPackageFileAsync("foo.json");
using var reader = new StreamReader(stream);
var fileContent = reader.ReadToEnd();

// For example deserialize JSON here.