I am trying to implement a very simple function in NET MAUI, however I am not able to do it. I have an excel file that I want to deploy as a resource on the target device and read later in the application.
In Windows App I can do this very easily by setting the Excel file as content and activating copy.
How does this work on Android? In some posts I have read that one should proceed as follows, yet it does not work:
- you open .csproj and define there.
<ItemGroup> <MauiAsset Include="ResourcesAssets*" /> </ItemGroup>
- then create a new folder Assets in the project under Platforms -> Android -> Resources and place the Excel file Info.xlsx there.
- set the build process for excel file as MauiAsset. I have also activated the copy here.
Now I read the Excel file in the program via the OpenXml library:
using (SpreadsheetDocument spreadSheetDocument = SpreadsheetDocument.Open(fileName, false))
I set the location of the file using:
string mainDir = FileSystem.Current.AppDataDirectory;
var fullpath = Path.Combine(mainDir, @"Info.xlsx");
When running, the debug reports that the file does not exist. If you check the data -> com.company… folder on the Android device, you will see only cash and no other folder!
By the way I tried something before and found out that you can’t create folders on an Android device via MAUI, so I wasn’t very surprised here when I only saw cash folders.
Am I doing something fundamentally wrong or is it MAUI bug? You read in some places already that MAUI is not redy for use and even worse that even basic things do not work.
I hope someone has an idea what is going on here.
Thanks
pcsasa
EDIT:
So, I’ve been working on the problem of reading a simple text file for a couple of hours now, and it’s been without result.
In summary:
- I noticed that Visual Studio also has a Resources folder in the project and a Raw folder under it. I now also have the same structure under Platforms -> Android then Resources -> Raw because I don’t know where the app is looking.
- in the folder I then placed an usual text file and set build to MauiAsset
- then I used from MAUI doc the code to read "Bundled files":
public async Task<string> ReadTextFile(string filePath) { using Stream fileStream = await FileSystem.Current.OpenAppPackageFileAsync(filePath); using StreamReader reader = new StreamReader(fileStream); return await reader.ReadToEndAsync(); }
- once I specified plain text file as filePath and when that didn’t work, I also supplied the path:
var fullpath = Path.Combine(@"Resources/Raw", @"AboutAssets.txt");
…but that doesn’t work either!
The thing that surprises me the most is that there is no error message, but the string set is equal to "" (so not even null).
I really can’t imagine that no one has included a file in the project until now to read it out in the application (e.g. image, settings, texts, Excel, etc.)? I wonder because there is really nothing in the MAUI documentation that describes how to read a file on an Android device except this "File system helpers" post. You can not find even in forums this question that I asked?
Is the mistake somewhere with me and I ask stupid questions, or do I try here something what does not go at all, because MAUI has a fundamental error in itself?
If my intention cannot be implemented in MAUI (i.e. simply include the file in the project and read it out on the target device), then that is a criterion for exclusion from using MAUI.
Thanks
pcsasa
2
Answers
To read a plain text file in Andriod, you could drag a TextFile.txt to Project/Resources/Raw folder and set its Build Action to MauiAsset.
You could check in the *.csproj file, please remove the following code if it exists
Then you could try reading the string in text file like this:
Hope it works for you.
Thanks Liqun Shen-MSFT
Since I have lost a lot of time and there are probably still people who have or will have the same problem, here again resumed how to include a resource (text or Excel) via NET MAUI and read out on the target device (eg an Android Phone) and use in the program:
So this worked very well in the Android emulator as well as on a physical device (Android Phone). If it works on the iPhone I couldn’t try it, but I would be very happy if someone could check it and write something here.
Thanks for the help.