Interacting with file and folder dialogs in .NET MAUI using CommunityToolkit.

 

Interacting with file and folder dialogs in .NET MAUI using CommunityToolkit.

Please, support my blog by clicking on our sponsors ad!

File and folder interactions are routine for many applications, often requiring the creation of cumbersome, platform-specific code.

The good news is that CommunityToolkit.Maui version 5.0 now includes enhanced FolderPicker and FileSaver classes, simplifying the process of selecting target folders and saving files across all .NET MAUI platforms.


FileSaver

Implementing FileSaver allows your application to present users with a user-friendly dialog for choosing a destination folder. With just a few lines of code, you can save various file types, such as documents, images, videos, and more.


Example in C#:

using var stream = new MemoryStream(Encoding.Default.GetBytes("Hello from the Community Toolkit!"));
var fileSaveResult = await FileSaver.Default.SaveAsync("sample.txt", stream, cancellationToken);
if (fileSaveResult.IsSuccessful)
{
    await Toast.Make($"File is saved: {fileSaveResult.FilePath}").Show(cancellationToken);
}
else
{
    await Toast.Make($"File is not saved, {fileSaveResult.Exception.Message}").Show(cancellationToken);
}


This code initiates a filesystem dialog for users to pick the target file location, creating a new file named "sample.txt" with the text "Hello from the Community Toolkit!" Users can customize the file name.

While FileSaver handles all exceptions and returns the operation result, selectively manage specific exceptions, like when the user cancels the operation, by wrapping the code in a try/catch block and calling the EnsureSuccess method:

CancellationTokenSource cancellationToken = new CancellationTokenSource();
using var stream = new MemoryStream(Encoding.Default.GetBytes("Hello from the Community Toolkit!"));
try
{
    var fileSaverResult = await FileSaver.SaveAsync("initial-path", "sample.txt", stream, cancellationToken.Token);
    fileSaverResult.EnsureSuccess();

    await Toast.Make($"File is saved: {fileSaverResult.FilePath}").Show(cancellationToken.Token);
}
catch (Exception ex)
{
    await Toast.Make($"File is not saved, {ex.Message}").Show(cancellationToken.Token);
}

Note: Android Developers should provide permission to work with file storage.

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


FolderPicker

Another robust feature in CommunityToolkit.Maui is FolderPicker, allowing users to select folders in the filesystem using a UI dialog. With FolderPicker, developers can easily retrieve information about the chosen folder, such as its name and path.


Example in C#:

var folderPickerResult = await folderPicker.PickAsync(cancellationToken);
if (folderPickerResult.IsSuccessful)
{
    await Toast.Make($"Folder picked: Name - {folderPickerResult.Folder.Name}, Path - {folderPickerResult.Folder.Path}", ToastDuration.Long).Show(cancellationToken);
}
else
{
    await Toast.Make($"Folder is not picked, {folderPickerResult.Exception.Message}").Show(cancellationToken);
}


This code prompts the user to select a folder, with the result stored in the "folderPickerResult" variable.

Once again, it's advisable to enclose the code in a try/catch block if you prefer using the EnsureSuccess method:

CancellationToken cancellationToken = new CancellationToken();
try
{
    var folderPickerResult = await FolderPicker.PickAsync(cancellationToken);
    if (folderPickerResult.IsSuccessful)
    {
        await Toast.Make($"Folder picked: Name - {folderPickerResult.Folder.Name}, Path - {folderPickerResult.Folder.Path}", ToastDuration.Long).Show(cancellationToken);
    }
}
catch (Exception ex)
{
    await Toast.Make($"Folder is not picked, {ex.Message}").Show(cancellationToken);
}

Note: Android Developers should provide permission to work with file storage.

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


FileSaver and FolderPicker are potent additions to the CommunityToolkit.Maui library, streamlining developers' interactions with the filesystem for effective file and folder management. Further information about these APIs is available in the documentation.

If you're a C# and .NET MAUI developer, explore these tools today to enhance integration with the OS filesystem.

Lastly, check the complete release notes for version 5.0 for additional valuable resources catering to .NET MAUI developers.

Comments

Popular posts from this blog

Explore the UI libraries available for .NET MAUI at no cost.

Push Notification using Firebase in xamarin form (Android and IOS)

School UI Design using xamarin form