0

I have referred this Folder Picker .NET MAUI Link for folder picker. but I am not able to specify the particular file type in folder picker.

Like if I specify type as .txt then it should only show the folder which contains .txt files likewise.

public async Task<string> PickFolder()
    {
        var folderPicker = new WindowsFolderPicker();
        // Might be needed to make it work on Windows 10
        folderPicker.FileTypeFilter.Add(".txt");

        // Get the current window's HWND by passing in the Window object
        var hwnd = ((MauiWinUIWindow)App.Current.Windows[0].Handler.PlatformView).WindowHandle;

        // Associate the HWND with the file picker
        WinRT.Interop.InitializeWithWindow.Initialize(folderPicker, hwnd);

        var result = await folderPicker.PickSingleFolderAsync();

        return result?.Path;
    }
  • Folders aren’t files… – Dai Nov 22 '22 at 07:58
  • I want folders which only contain particular type of file. Like If any folder don't contain that type of file then it is not suppose to show me in my folder picker. – Preeti Jadhav Nov 22 '22 at 08:46
  • That isn’t possible with Win32’s folder pickers, sorry. What you can do is validate the selected folder and re-open the dialog if the user selected a bad directory. – Dai Nov 22 '22 at 08:54
  • You want to pick certain types of files through `FolderPicker`. And this hasn't been implemented and is a [feature request](https://github.com/CommunityToolkit/Maui/discussions/381), you can add your request to it and follow up there. Thanks! – Alexandar May - MSFT Nov 23 '22 at 09:21

1 Answers1

0

I have used Folder picker and given condition which checks the extensionand it is working the way I want.

 var pickedFolder = await _folderPicker.PickFolder();
        var files = Directory.GetFiles(pickedFolder);
        foreach(var file in files)
        {
            FileInfo fileInfo = new FileInfo(file);
            if(fileInfo.Extension==".txt")
            {
                Nodes = new ObservableCollection<TreeViewNode>(
                GetNodes(pickedFolder));
            }
        }