I created an appsettings file for a MAUI app and loading it in the IConfiguration using .Host.ConfigureAppConfiguration
on the builder from a MauiApp.CreateBuilder(); I can access the file in Windows but not when running the Android emulator. The code:
var builder = MauiApp.CreateBuilder();
builder
.RegisterBlazorMauiWebView()
.UseMauiApp<App>()
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
})
.Host
.ConfigureAppConfiguration((app, config) =>
{
#if __ANDROID__
// https://mcmap.net/q/1322622/-accessing-files-through-a-physical-path-in-xamarin-android
//var documentsFolderPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments);
config.AddJsonFile( "appsettings.json", optional: false, reloadOnChange: true);
#endif
#if WINDOWS10_0_17763_0_OR_GREATER
//https://mcmap.net/q/1322623/-how-to-load-app-configuration-from-appsettings-json-in-maui-startup
Assembly callingAssembly = Assembly.GetEntryAssembly();
Version versionRuntime = callingAssembly.GetName().Version;
string assemblyLocation = Path.GetDirectoryName(System.AppContext.BaseDirectory); //CallingAssembly.Location
var configFile = Path.Combine(assemblyLocation, "appsettings.json");
config.AddJsonFile(configFile, optional: false, reloadOnChange: true);
#endif
});
Mockup project is here