title | author | description | ms.date |
---|---|---|---|
FolderPicker - .NET MAUI Community Toolkit |
VladislavAntonyuk |
The FolderPicker provides the ability to pick a folder from the file system. |
12/11/2022 |
The FolderPicker
provides the ability to pick a folder from the file system.
The following preconditions required for the FolderPicker
:
Add permissions to AndroidManifest.xml
:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Nothing is needed.
When using App Sandbox, which is required if you wish to distribute your macOS application via the Apple App Store, you will also need to enable Accessing files from the macOS App Sandbox.
There are two key options when enabling access to files from the macOS App Sandbox:
- Read-only access
Add the following into your entitlements.plist file:
<key>com.apple.security.files.user-selected.read-only</key>
<true/>
Further reading at: https://developer.apple.com/documentation/bundleresources/entitlements/com_apple_security_files_user-selected_read-only
- Read-write access
Add the following into your entitlements.plist file:
<key>com.apple.security.files.user-selected.read-write</key>
<true/>
Further reading at: https://developer.apple.com/documentation/bundleresources/entitlements/com_apple_security_files_user-selected_read-write
Nothing is needed.
Add permissions to tizen-manifest.xml
:
<privilege>http://tizen.org/privilege/mediastorage</privilege>
<privilege>http://tizen.org/privilege/externalstorage</privilege>
The FolderPicker
can be used as follows in C#:
async Task PickFolder(CancellationToken cancellationToken)
{
var result = await FolderPicker.Default.PickAsync(cancellationToken);
if (result.IsSuccessful)
{
await Toast.Make($"The folder was picked: Name - {result.Folder.Name}, Path - {result.Folder.Path}", ToastDuration.Long).Show(cancellationToken);
}
else
{
await Toast.Make($"The folder was not picked with error: {result.Exception.Message}").Show(cancellationToken);
}
}
The Folder
record represents a folder in the file system. It defines the following properties:
- Path contains a Folder path.
- Name contains a Folder name.
Stores information from PickAsync
.
Property | Type | Description |
---|---|---|
Folder | Folder |
Gets the Folder that represents the selected folder in the file system. |
Exception | Exception |
Gets the Exception if the pick operation failed. |
IsSuccessful | bool |
Gets a value determining whether the operation was successful. |
Method | Description |
---|---|
EnsureSuccess | Verifies whether the pick operation was successful. |
Warning
EnsureSuccess
will throw an Exception
if the pick operation was unsuccessful.
Method | Description |
---|---|
PickAsync | Asks for permission and allows selecting a folder in the file system. |
In case you want to inject 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();
// Register the FolderPicker as a singleton
builder.Services.AddSingleton<IFolderPicker>(FolderPicker.Default);
// Register the MainPage as transient to make sure it can resolve the IFolderPicker dependency.
builder.Services.AddTransient<MainPage>();
return builder.Build();
}
}
Now you can inject the service like this:
public partial class MainPage : ContentPage
{
private readonly IFolderPicker folderPicker;
public MainPage(IFolderPicker folderPicker)
{
InitializeComponent();
this.folderPicker = folderPicker;
}
async Task PickFolder(CancellationToken cancellationToken)
{
var result = await folderPicker.PickAsync(cancellationToken);
result.EnsureSuccess();
await Toast.Make($"Folder picked: Name - {result.Folder.Name}, Path - {result.Folder.Path}", ToastDuration.Long).Show(cancellationToken);
}
}
For more detail on how to provide a CancellationToken
refer to the Microsoft documentation.
You can find an example of FolderPicker
in action in the .NET MAUI Community Toolkit Sample Application.
You can find the source code for FolderPicker
over on the .NET MAUI Community Toolkit GitHub repository.