使用FolderPicker时显示文件夹中的文件

时间:2019-04-25 07:33:51

标签: c# uwp

使用FolderPicker时是否可以通过任何方式显示文件夹中的文件?

代码:

FolderPicker folderPicker = new FolderPicker();
folderPicker.ViewMode = PickerViewMode.Thumbnail;
folderPicker.SuggestedStartLocation = PickerLocationId.ComputerFolder;
folderPicker.FileTypeFilter.Add("*");

return await folderPicker.PickSingleFolderAsync();

2 个答案:

答案 0 :(得分:0)

如果使用WindowsAPICodepack,则可以这样做:

var openFolder = new CommonOpenFileDialog();
openFolder.AllowNonFileSystemItems = true;
openFolder.Multiselect = true;
openFolder.IsFolderPicker = true;
openFolder.Title = "Select folders.";

if (openFolder.ShowDialog() != CommonFileDialogResult.Ok)
{
    MessageBox.Show("No folder selected.");
    return;
}

// get all the directories in selected directory
var dirs = openFolder.FileNames.ToArray();

但是,如果您仅使用Windows.Storage.Pickers,则提供的代码就是Microsoft文档(https://docs.microsoft.com/en-us/uwp/api/windows.storage.pickers.folderpicker)上给出的示例,因此我不太确定为什么不这样做工作。

答案 1 :(得分:0)

对于内置的FolderPicker,这是不可能的。您可以调用“ StorageFolder.GetFilesAsync”方法来获取当前文件夹中的文件。

FolderPicker folderPicker = new FolderPicker();
folderPicker.ViewMode = PickerViewMode.List;
folderPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
folderPicker.FileTypeFilter.Add("*");

var folder = await folderPicker.PickSingleFolderAsync();

foreach (var files in await folder.GetFilesAsync())
{
    Debug.WriteLine(files.DisplayName);
}

如果要在UI上显示这些文件,则可以选择一些列表控件以显示它们。例如,List view and grid view