'Problem with configuration in .NET,problem with read a json file
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:
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);
}
}
}
}
Solution 1:[1]
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 GetExecutingAssembly
.SetBasePath(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location))
Using GetEntryAssembly
.SetBasePath(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location))
The GetEntryAssembly version is really only necessary if you're building config in a library or package
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |



