Reputation: 339
I am converting my Xamarin Forms Application to .NET MAUI.
In existing application I am using below code which is using Xamarin.iOS to fetch the config file(App.config) which is in xml format and has app settings
public string Path
{
get
{
try
{
return System.AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
}
catch
{
throw new FileNotFoundException(@"File not found.");
}
}
}
But the same code doesn't work in MAUI
I tried below approach but File.OpenRead(Path) is throwing error "Data at the root level is invalid. Line 1, position 1" App.config file is added in path Projectname/Platforms/iOS/App.config
public string Path
{
get
{
try
{
string configPath = string.Empty;
configPath = Assembly.GetEntryAssembly().Location + ".config";
AppDomain domain = System.AppDomain.CurrentDomain;
configPath = System.IO.Path.Combine(domain.SetupInformation.ApplicationBase, domain.FriendlyName + ".config");
return configPath;
}
catch
{
throw new FileNotFoundException(@"File not found.");
}
}
}
Any help is appreciated!
Upvotes: 0
Views: 1831
Reputation: 8245
Since the value of Path could be obtained, you could manually checked the content to see if it is the one you want.
From the thrown error "Data at the root level is invalid. Line 1, position 1", it seems something wrong with xml data load. Some issues similar to this question indicated that it may due to BOM character, take a look at this issue.
Actually we could use appsettings.json as an alternative, see this App Configuration Settings in .NET MAUI and github project. It's really easy to read app config in this way.
Upvotes: 0