Share via

How to Read Files inside the folder in maui blazor android

Prathamesh Shende 511 Reputation points
2026-03-24T04:38:52.2233333+00:00

I have created a project in Maui Blazor Hybrid. I am exploring the folder picker service in CommunityToolkit. Maui. I want to read the files inside the folder picked.

Here is my code.

I have also tried with the Home.razor component page, and I always find the file count to be 0. No exception or error gets thrown in Android.

I have given the permissions to access the storage and read audio, image, video.

 private async void OnCounterClicked(object sender, EventArgs e)
 {
     try
     {
         var folder = await FolderPicker.PickAsync(default);

         folderPath.Text = $"Name: {folder.Folder.Name} Path: {folder.Folder.Path}";

         var files = Directory
            .EnumerateFiles(folder.Folder.Path, "*.*", SearchOption.AllDirectories);

         int count = files.Count();
     }
     catch
     {
         // TODO Implement
     }
 }

Developer technologies | .NET | .NET MAUI
0 comments No comments

3 answers

Sort by: Most helpful
  1. Nancy Vo (WICLOUD CORPORATION) 2,305 Reputation points Microsoft External Staff Moderator
    2026-03-24T08:27:38.4466667+00:00

    Hi @Prathamesh Shende ,

    Thanks for your question.

    The reason count is always 0 on Android is because the folder returned by the FolderPicker is not a real file system path. On Android, folder access is provided through the Storage Access Framework (SAF), which gives your app a URI-based permission, not full file system access.

    Because of this, APIs like Directory.EnumerateFiles will not work in this scenario and will return empty results.

    I recommend these solutions and example codes:

    • To help users avoid picking the folder again, you store and persist the URI permission.
    var uri = Android.Net.Uri.Parse(folder.Folder.Path);
    
    Platform.CurrentActivity.ContentResolver.TakePersistableUriPermission(
        uri,
        ActivityFlags.GrantReadUriPermission | ActivityFlags.GrantWriteUriPermission
    );
    
    // Save uri.ToString() somewhere (Preferences / database)
    
    • When the app restarts:
    var savedUri = Android.Net.Uri.Parse(savedUriString);
    

    This allows your app to access the same folder again without asking the user.

    • Instead of Directory.EnumerateFiles, use Android’s ContentResolver:
    var childrenUri = DocumentsContract.BuildChildDocumentsUriUsingTree(
        savedUri,
        DocumentsContract.GetTreeDocumentId(savedUri)
    );
    
    var cursor = Platform.CurrentActivity.ContentResolver.Query(
        childrenUri,
        null,
        null,
        null,
        null
    );
    
    while (cursor.MoveToNext())
    {
        var documentId = cursor.GetString(0);
    
        var documentUri = DocumentsContract.BuildDocumentUriUsingTree(
            savedUri,
            documentId
        );
    
    }
    

    However, I would not recommend doing this because once the app has permission to access the folder, it can view its contents, which poses a security risk. Instead, you pick files directly with FilePicker without reading files inside the folder picked.

    I hope this addresses your question. If this response was helpful, please consider following the guidance to provide feedback


  2. Prathamesh Shende 511 Reputation points
    2026-03-24T05:59:09.0966667+00:00

    So the user always has to pick up the files if the folder contains more files or the user closes the app and reopens it.
    Just like Directory, it will scan each file from the folder, even if newly added or deleted.

    0 comments No comments

  3. Q&A Assist
    2026-03-24T04:39:08.68+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    On Android, the folder path returned by CommunityToolkit.Maui.Storage.FolderPicker does not point to a location that can be enumerated with System.IO.Directory.EnumerateFiles. The picker grants access via a storage URI/permission, not a normal, recursively readable filesystem directory.

    Because of this platform limitation, Directory.EnumerateFiles(folder.Folder.Path, ...) will return zero items on Android even though a folder was selected.

    Use these approaches instead:

    1. Prefer picking files directly with FilePicker

    If the goal is to let the user select files, use .NET MAUI IFilePicker / FilePicker.Default rather than a folder picker, and work with the returned FileResult objects:

    using Microsoft.Maui.Storage;
    
    var results = await FilePicker.Default.PickMultipleAsync();
    if (results != null)
    {
        foreach (var file in results)
        {
            using var stream = await file.OpenReadAsync(); // works on all platforms
            // process file
        }
    }
    

    On Android, always use OpenReadAsync() instead of File.OpenRead(result.FullPath) because FullPath may be a content:// URI, not a real path.

    1. If a folder is required, treat it as a user-selected root and let the user pick files inside it

    On Android, do not rely on Directory.EnumerateFiles over folder.Folder.Path. Instead, after the folder is chosen, prompt the user again with FilePicker scoped to that area (where possible) or design the UX so the user picks individual files rather than expecting recursive enumeration.

    1. Ensure permissions are correct (but note they do not fix enumeration)

    For Android 12 and lower, request READ_EXTERNAL_STORAGE. For Android 13 and higher, request the granular media permissions (READ_MEDIA_IMAGES, READ_MEDIA_VIDEO, READ_MEDIA_AUDIO) as documented. These are required for file access but still do not change the fact that the folder picker’s path cannot be reliably enumerated with Directory.EnumerateFiles.

    In summary, on Android with MAUI and CommunityToolkit FolderPicker, enumerating all files in the picked folder via Directory.EnumerateFiles is not supported. Use FilePicker and OpenReadAsync to work with user-selected files instead of trying to walk the folder tree.


    References:

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.