How do you read and write files in MAUI cross-platform?
Short answer
Use System.IO with paths from FileSystem.AppDataDirectory (private, persistent) or CacheDirectory (disposable). For files bundled with the app, use FileSystem.OpenAppPackageFileAsync since packaged assets aren't regular filesystem paths on all platforms.
Advertisement
AModel answer
Use System.IO with paths from FileSystem.AppDataDirectory (private, persistent) or CacheDirectory (disposable). For files bundled with the app, use FileSystem.OpenAppPackageFileAsync since packaged assets aren't regular filesystem paths on all platforms.
string path = Path.Combine(FileSystem.AppDataDirectory, "data.json");
await File.WriteAllTextAsync(path, json);
using var stream = await FileSystem.OpenAppPackageFileAsync("seed.json");Real-world example
An app writes user data to AppDataDirectory and reads a bundled seed file via OpenAppPackageFileAsync on first run.
Likely follow-ups
- AppDataDirectory vs CacheDirectory? | How do you read a packaged file?
Related topics