Package and Read Asset Files in .NET MAUI

Learn to package raw asset files (`MauiAsset`) in .NET MAUI and read them using `OpenAppPackageFileAsync`, with a code example.

  1. Place the files to be packaged in the Resources\Raw\ directory.

  2. Set the file’s Build Action to MauiAsset.

  3. Use using (Stream instream = FileSystem.Current.OpenAppPackageFileAsync(fileName)) to read the file.

  4. Copy the file to another directory.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    
    // Open the source file
    using Stream inputStream = await FileSystem.Current.OpenAppPackageFileAsync(filename);
    
    // Create an output filename
    string targetFile = Path.Combine(FileSystem.Current.AppDataDirectory, filename);
    
    // Copy the file to the AppDataDirectory
    using FileStream outputStream = File.Create(targetFile);
    await inputStream.CopyToAsync(outputStream);
    
Built with Hugo
Theme Stack designed by Jimmy