I’m writing a .NET MAUI app that loads Bible verses from a text file. Works fine when running on Windows, but the text file doesn’t appear when running the app in the Android Emulator, even though I have the text file’s Copy to output directory option
set to Copy always
. Instead, once the build finishes I get a TargetInvocationException
because the file doesn’t appear in the App Data Directory. How do I make sure the file is in the right place when running the app on Android?
I have this piece of code for loading the file:
string filePath = Path.Combine(FileSystem.Current.AppDataDirectory, "erv.txt");
Console.WriteLine(filePath);
if (!File.Exists(filePath)) {
throw new FileNotFoundException("erv.txt not found", filePath);
}
using (StreamReader sr = File.OpenText(filePath))
I tried adding this piece of code to the project file
<Content Include="Resourceserv.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
But it seems to make no difference.