Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to bundle files in MAUI?

I'm trying to access files in MAUI and I know I should bundle them but I don't know how to do it. In what directory should I place the file? When I bundle it, how should I write the path of the file?

like image 881
CitrusBoy Avatar asked Nov 06 '25 21:11

CitrusBoy


1 Answers

  1. Place the files you want to bundle in Resources\Raw\ folder within your project. I could not get other locations to work.

  2. Mark the files with Build Action = MauiAsset, and select an option for Copy to Output Directory (always or newer)

  3. access them by relative path only (so for most files you will only use the raw file name):

    using (Stream instream = FileSystem.Current.OpenAppPackageFileAsync(fileName))

  4. read the contents with StreamReader or BinaryReader.

I needed to copy the files to my app data directory and what did NOT work was using a ByteArrayContent.ReadAsByteArray (it hangs) and instream.CopyToAsync (it also hangs). To copy files you could use the approach described here:

.Net Maui copying assets into appData

Based on your comment, you don't want to use async, but there is only OpenAppPackageFileAsync available.

you could put this in an async function

async Task GetMyAssets() { ... }

then call it from a synchronous function like this:

GetMyAssets().GetAwaiter().GetResult();
like image 171
sema4 Avatar answered Nov 09 '25 17:11

sema4



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!