.net 如何在文件夹选取器中指定文件类型

cyvaqqii  于 2022-11-26  发布在  .NET
关注(0)|答案(1)|浏览(132)

我已为文件夹选取器引用此Folder Picker .NET MAUI链接。但我无法在文件夹选取器中指定特定的文件类型。
就像如果我指定类型为.txt,那么它应该只显示包含**.txt**文件的文件夹同样。

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;
    }
4ioopgfo

4ioopgfo1#

我已经使用了文件夹选择器和给定的条件,检查扩展名,它是工作的方式,我想要的。

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));
            }
        }

相关问题