Training
Learning path
Access local files asynchronously - Training
Learn how to manage local files using the System.IO namespace and how to asynchronously back up and restore application data using the System.Text.Json namespace.
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
The FileSaver
provides the ability to select a target folder and save files to the file system.
The following preconditions are required for the FileSaver
:
If your target device API level is less than 33 add permissions to AndroidManifest.xml
:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
When your app targets Android API level 34 and up, no additional permissions are required.
For more information about Android storage permission, please refer to the Android documentation about Manifest.permission
The FileSaver
can be used as follows in C#:
async Task SaveFile(CancellationToken cancellationToken)
{
using var stream = new MemoryStream(Encoding.Default.GetBytes("Hello from the Community Toolkit!"));
var fileSaverResult = await FileSaver.Default.SaveAsync("test.txt", stream, cancellationToken);
if (fileSaverResult.IsSuccessful)
{
await Toast.Make($"The file was saved successfully to location: {fileSaverResult.FilePath}").Show(cancellationToken);
}
else
{
await Toast.Make($"The file was not saved successfully with error: {fileSaverResult.Exception.Message}").Show(cancellationToken);
}
}
or in case the file is rather big and takes some time to be saved, you may be interested to know the progress:
async Task SaveFile(CancellationToken cancellationToken)
{
using var stream = new MemoryStream(Encoding.Default.GetBytes("Hello from the Community Toolkit!"));
var saverProgress = new Progress<double>(percentage => ProgressBar.Value = percentage);
var fileSaverResult = await FileSaver.Default.SaveAsync("test.txt", stream, saverProgress, cancellationToken);
if (fileSaverResult.IsSuccessful)
{
await Toast.Make($"The file was saved successfully to location: {fileSaverResult.FilePath}").Show(cancellationToken);
}
else
{
await Toast.Make($"The file was not saved successfully with error: {fileSaverResult.Exception.Message}").Show(cancellationToken);
}
}
Method | Description |
---|---|
SaveAsync | Asks for permission, allows selecting a folder and saving files to the file system. |
The result returned from the SaveAsync
method. This can be used to verify whether the save was successful, check where the file was saved, and also access any exceptions that may have occurred during the save.
Property | Type | Description |
---|---|---|
FilePath | string |
The location on the disk where the file was saved. |
Exception | Exception |
Gets the Exception if the save operation fails. |
IsSuccessful | bool |
Gets a value determining whether the operation was successful. |
Method | Description |
---|---|
EnsureSuccess | Verifies whether the save operation was successful. |
Warning
EnsureSuccess
will throw an Exception
if the save operation is unsuccessful.
In case you want to inject a service, you first need to register it.
Update MauiProgram.cs
with the next changes:
public static class MauiProgram
{
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.UseMauiCommunityToolkit();
builder.Services.AddSingleton<IFileSaver>(FileSaver.Default);
return builder.Build();
}
}
Now you can inject the service like this:
public partial class MainPage : ContentPage
{
private readonly IFileSaver fileSaver;
public MainPage(IFileSaver fileSaver)
{
InitializeComponent();
this.fileSaver = fileSaver;
}
public async void SaveFile(object sender, EventArgs args)
{
using var stream = new MemoryStream(Encoding.Default.GetBytes("Hello from the Community Toolkit!"));
var fileSaverResult = await fileSaver.SaveAsync("test.txt", stream, cancellationToken);
fileSaverResult.EnsureSuccess();
await Toast.Make($"File is saved: {fileSaverResult.FilePath}").Show(cancellationToken);
}
}
You can find an example of FileSaver
in action in the .NET MAUI Community Toolkit Sample Application.
You can find the source code for FileSaver
over on the .NET MAUI Community Toolkit GitHub repository.
.NET MAUI Community Toolkit feedback
.NET MAUI Community Toolkit is an open source project. Select a link to provide feedback:
Training
Learning path
Access local files asynchronously - Training
Learn how to manage local files using the System.IO namespace and how to asynchronously back up and restore application data using the System.Text.Json namespace.