All skills
davidortinau avatar

/maui-file-handling

@542317e

Guidance for file picker, file system helpers, bundled assets, and app data storage in .NET MAUI applications. Covers FilePicker APIs, FileResult handling, platform permissions, and common pitfalls across Android, iOS, macOS, and Windows. USE FOR: "file picker", "FilePicker", "pick file", "open file", "save file", "bundled assets", "FileSystem helpers", "AppDataDirectory", "CacheDirectory", "FileResult", "read file MAUI". DO NOT USE FOR: media capture or photo picking (use maui-media-picker), secure credential storage (use maui-secure-storage), or SQLite database files (use maui-sqlite-database).

Use this Skill: https://skilld.dev/gh/davidortinau/maui-skills/maui-file-handling

This session only. Nothing lands on disk.

referencesfile-handling-api.md

≈1.3k tokens on demand. Your agent reads this file only when SKILL.md points to it.

File Handling API Reference

FilePicker API

Use FilePicker.Default to let users select files from the device.

Single file

var result = await FilePicker.Default.PickAsync(new PickOptions
{
    PickerTitle = "Select a file",
    FileTypes = FilePickerFileType.Images
});

if (result is not null)
{
    using var stream = await result.OpenReadAsync();
    // process stream
}

Multiple files

var results = await FilePicker.Default.PickMultipleAsync(new PickOptions
{
    PickerTitle = "Select files",
    FileTypes = FilePickerFileType.Videos
});

foreach (var file in results)
{
    // file.FileName, file.FullPath, file.ContentType
}

PickOptions

Property Type Purpose
PickerTitle string Title shown on the picker dialog
FileTypes FilePickerFileType Restricts selectable file types

FilePickerFileType

Built-in types

  • FilePickerFileType.Images — common image formats
  • FilePickerFileType.Png — PNG only
  • FilePickerFileType.Jpeg — JPEG only
  • FilePickerFileType.Videos — common video formats
  • FilePickerFileType.Pdf — PDF files

Custom per-platform type

var customFileType = new FilePickerFileType(
    new Dictionary<DevicePlatform, IEnumerable<string>>
    {
        { DevicePlatform.Android, new[] { "application/json", "text/plain" } },   // MIME types
        { DevicePlatform.iOS, new[] { "public.json", "public.plain-text" } },     // UTTypes
        { DevicePlatform.macOS, new[] { "public.json", "public.plain-text" } },   // UTTypes
        { DevicePlatform.WinUI, new[] { ".json", ".txt" } }                       // file extensions
    });

FileResult

Returned by PickAsync and PickMultipleAsync.

Property Type Notes
FullPath string Platform-specific absolute path
FileName string File name with extension
ContentType string MIME type of the file
OpenReadAsync() Task<Stream> Preferred way to read file contents

FileSystem Helpers

Access via FileSystem.Current.

Directory paths

Property Purpose Writable
CacheDirectory Temp/cache data Yes
AppDataDirectory Persistent app-private data Yes

Reading bundled files

using var stream = await FileSystem.Current.OpenAppPackageFileAsync("data.json");
using var reader = new StreamReader(stream);
string contents = await reader.ReadToEndAsync();

Bundled Files (Resources/Raw)

Place files in the Resources/Raw folder. They receive the MauiAsset build action automatically.

  • Files are read-only at runtime.
  • Access via OpenAppPackageFileAsync("filename.ext").
  • Subdirectories are flattened on some platforms—use unique file names.

Copy bundled file to writable location

public async Task<string> CopyToAppDataAsync(string filename)
{
    string targetPath = Path.Combine(FileSystem.Current.AppDataDirectory, filename);

    if (!File.Exists(targetPath))
    {
        using var source = await FileSystem.Current.OpenAppPackageFileAsync(filename);
        using var dest = File.Create(targetPath);
        await source.CopyToAsync(dest);
    }

    return targetPath;
}

Permissions

Android

Android version Permission required
≤ 12 (API 32) READ_EXTERNAL_STORAGE
≥ 13 (API 33) READ_MEDIA_IMAGES, READ_MEDIA_VIDEO, READ_MEDIA_AUDIO (granular)

Declare in Platforms/Android/AndroidManifest.xml. Request at runtime with Permissions.RequestAsync<Permissions.StorageRead>() or the granular media permissions.

iOS

  • FilePicker works without special permissions for on-device files.
  • For iCloud access, enable the iCloud capability and configure entitlements.

macOS (Mac Catalyst)

  • Enable App Sandbox entitlements.
  • Grant com.apple.security.files.user-selected.read-only (or read-write) for picker access.

Windows

  • Packaged apps have full picker access without extra declarations.

Platform Path Differences

Platform AppDataDirectory location CacheDirectory location
Android /data/data/<package>/files /data/data/<package>/cache
iOS / macOS <app-sandbox>/Library/ <app-sandbox>/Library/Caches/
Windows <LocalAppData>/<PackageName>/LocalState <LocalAppData>/<PackageName>/LocalCache

Source: SKILL.md on GitHub

No alerts15d4 checks · Risk SAFE
  • Gen Agent Trust Hub15d

    This skill provides educational guidance and API references for handling files in .NET MAUI applications across various platforms (Android, iOS, macOS, Windows). It covers best practices for using the FilePicker, platform-specific path handling, and managing app data directories. No malicious patterns or security risks were detected.

  • Socket15d

    No alerts

  • Snyk15d

    Risk: LOW · No issues

  • Runlayer7mo

    1 file scanned · No issues

Signed by skilld at 542317e. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 2 months ago.

Steadyupdated 6 months ago

README badge

README badge for davidortinau/maui-skills/maui-file-handling