.NET MAUI Accessing Files and Folders Cross Platform Step by step Implementation and Top 10 Questions and Answers
 Last Update: April 01, 2025      9 mins read      Difficulty-Level: beginner

.NET MAUI Accessing Files and Folders Cross-Platform: A Step-by-Step Guide

As developers looking to create cross-platform applications with .NET MAUI (Multi-platform App UI), one of the most critical tasks is managing file and folder access. This guide will walk you through the process of accessing files and folders in a cross-platform manner using .NET MAUI, ensuring your application runs smoothly on Windows, macOS, iOS, and Android.

Prerequisites

Before we dive into accessing files and folders, ensure you have the following prerequisites:

  • Visual Studio: Version 2022 or later with the .NET Multi-platform App UI development workload installed.
  • .NET MAUI SDK: Ensure you have the latest .NET MAUI SDK installed.

Once you have these ready, let's begin.

Understanding File System Access in .NET MAUI

.NET MAUI provides a cross-platform API for accessing the file system, making it simpler to handle files and folders across different platforms. The FileSystem class is the primary entry point for file and folder operations.

Step 1: Adding Permissions (Android and iOS)

Before you start accessing files, ensure your application has the necessary permissions.

Android:

  1. Open your Platforms/Android/AndroidManifest.xml.

  2. Add the following permissions inside the <manifest> tag:

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    

    For Android 10 and above, also include:

    <application android:requestLegacyExternalStorage="true" ... >
    

iOS:

  1. Open your Platforms/iOS/Info.plist.
  2. Add the following keys inside the <dict> tag to request permissions:
    <key>NSPhotoLibraryAddUsageDescription</key>
    <string>This app needs access to your photo library to save files.</string>
    <key>NSPhotoLibraryUsageDescription</key>
    <string>This app needs access to your photo library to access files.</string>
    

These permissions are essential for reading from and writing to the file system on Android and iOS devices.

Step 2: Accessing the Application's Internal Storage

The simplest way to store files is within your application's internal storage. This storage is specific to your app and is automatically cleared when the app is uninstalled.

Reading from Internal Storage:

var fileName = "myfile.txt";
var path = Path.Combine(FileSystem.Current.AppDataDirectory, fileName);

string content;
using (var stream = await FileSystem.Current.OpenAppPackageFileAsync(fileName))
using (var reader = new StreamReader(stream))
{
    content = await reader.ReadToEndAsync();
}

Writing to Internal Storage:

var fileName = "myfile.txt";
var path = Path.Combine(FileSystem.Current.AppDataDirectory, fileName);

using (var stream = await FileSystem.Current.OpenAppPackageFileAsync(fileName, FileSystemOpenMode.Write))
using (var writer = new StreamWriter(stream))
{
    await writer.WriteAsync("Hello, .NET MAUI!");
}

Step 3: Accessing External Storage

Accessing external storage, such as the SD card or external drives, requires additional handling and permissions.

Reading from External Storage:

var fileName = "myfile.txt";
var path = Path.Combine(FoldPath.RootDirectory, "Documents", fileName);

string content;
using (var stream = File.OpenRead(path))
using (var reader = new StreamReader(stream))
{
    content = await reader.ReadToEndAsync();
}

Writing to External Storage:

var fileName = "myfile.txt";
var path = Path.Combine(FoldPath.RootDirectory, "Documents", fileName);

if (!Directory.Exists(Path.GetDirectoryName(path)))
{
    Directory.CreateDirectory(Path.GetDirectoryName(path));
}

using (var stream = File.OpenWrite(path))
using (var writer = new StreamWriter(stream))
{
    await writer.WriteAsync("Hello, .NET MAUI!");
}

Step 4: Platform-Specific Storage Access

While .NET MAUI provides a unified API for file and folder operations, there may be scenarios where platform-specific access is necessary.

Android:

var path = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath;

iOS:

var path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

Step 5: Handling Exceptions

File and folder operations can often lead to exceptions, especially when dealing with external storage. Ensure you handle exceptions gracefully to provide a better user experience.

try
{
    // file operation code here
}
catch (Exception ex)
{
    Console.WriteLine($"An error occurred: {ex.Message}");
}

Step 6: Using the FileBase Class

The FileBase class provides a more abstracted way to handle files. You can create instances of FileBase to perform read and write operations.

var fileName = "myfile.txt";
var path = Path.Combine(FileSystem.Current.AppDataDirectory, fileName);

// Creating an instance of FileBase for reading
var file = await FileSystem.Current.GetFileFromPathAsync(path);
using (var stream = await file.OpenAsync(FileAccess.Read))
using (var reader = new StreamReader(stream))
{
    string content = await reader.ReadToEndAsync();
}

// Creating an instance of FileBase for writing
file = await FileSystem.Current.GetFileFromPathAsync(path, true);
using (var stream = await file.OpenAsync(FileAccess.Write))
using (var writer = new StreamWriter(stream))
{
    await writer.WriteAsync("Hello, .NET MAUI!");
}

Step 7: Using FolderBase Class

Similar to FileBase, the FolderBase class allows you to perform operations on folders.

var folderPath = FileSystem.Current.AppDataDirectory;
var folder = await FileSystem.Current.GetFolderFromPathAsync(folderPath);

// Create a new folder
var newFolder = await folder.CreateFolderAsync("NewFolder", CreationCollisionOption.FailIfExists);

// Enumerate all files in a folder
IEnumerable<FileBase> files = await folder.GetFilesAsync();
foreach (var file in files)
{
    Console.WriteLine(file.Name);
}

// Enumerate all subfolders in a folder
IEnumerable<FolderBase> subfolders = await folder.GetFoldersAsync();
foreach (var subfolder in subfolders)
{
    Console.WriteLine(subfolder.Name);
}

Conclusion

Accessing files and folders in .NET MAUI is a straightforward process thanks to the unified API provided by the FileSystem class. By understanding file permissions, internal and external storage, platform-specific access, and exception handling, you can confidently manage file and folder operations in your cross-platform applications.

Remember, proper file management is crucial for the stability and security of your applications. Always ensure you handle user data with care and provide clear explanations for any requested permissions. Happy coding!


This guide provides a thorough approach to file and folder management in .NET MAUI, covering the necessary permissions, internal and external storage, and best practices for exception handling. By following these steps, you should be able to manage files and folders effectively in your .NET MAUI applications.