简体   繁体   English

Directory.EnumerateFiles在UWP中返回0个文件

[英]Directory.EnumerateFiles returns 0 files in UWP

In my console application I use 在控制台应用程序中,我使用

var allFiles = Directory.EnumerateFiles(directoryPath, "*.*", SearchOption.AllDirectories).ToList();

to return the path of each file in a folder (and in all sub-folders). 返回文件夹(以及所有子文件夹)中每个文件的路径。

However in UWP, using the same thing returns 0 但是在UWP中,使用相同的东西将返回0

    FolderPicker folderPicker = new FolderPicker();
    folderPicker.SuggestedStartLocation = PickerLocationId.ComputerFolder;
    folderPicker.FileTypeFilter.Add("*");
    StorageFolder pickedFolder = await folderPicker.PickSingleFolderAsync();
    var allFiles = Directory.EnumerateFiles(pickedFolder.Path, "*.*", SearchOption.AllDirectories).ToList();

I've made a method that uses the GetFilesAsync() and GetFolderAsync() functions but it is no where near as quick as Directory.EnumerateFiles() 我已经制作了一种使用GetFilesAsync()GetFolderAsync()函数的方法,但是它与Directory.EnumerateFiles()一样快。

 private async Task GetFilesInFolders(ObservableCollection<string> list, StorageFolder parent)
    {
        foreach (var file in await parent.GetFilesAsync())
        {
            list.Add(file.Path);
        }
        foreach (var folder in await parent.GetFoldersAsync())
        {
            await GetFilesInFolders(list, folder);
        }
    }

Why does Directory.EnumerateFiles() returns 0 files? 为什么Directory.EnumerateFiles()返回0个文件?

Unlike the traditional desktop application, UWP runs sandboxed and have very limited access to the file system. 与传统的桌面应用程序不同,UWP运行沙箱,并且对文件系统的访问非常有限。

By default the UWP can only access its Local Folder like the LocalFolder/InstallationFolder... or the files and folders in the user's Downloads folder that your app created, if apps need to access others fils, we may need to use capabilities or file picker. 默认情况下,UWP仅可以访问其本地文件夹(如LocalFolder / InstallationFolder ...)或您的应用程序创建的用户的Downloads文件夹中的文件和文件夹,如果应用程序需要访问其他文件,我们可能需要使用功能或文件选择器。 For more information, please see File access permissions 有关更多信息,请参见文件访问权限。

Why does Directory.EnumerateFiles() returns 0 files? 为什么Directory.EnumerateFiles()返回0个文件?

UWP app has no direct access to the folder by using the pickedFolder.Path if the pickedFolder is not in the app's local folder. 如果pickedFolder不在应用程序的本地文件夹中,则UWP应用程序无法通过使用pickedFolder.Path直接访问该文件夹。 If you pick the folder from the app's local folder, your code will work fine. 如果您从应用程序的本地文件夹中选择该文件夹,则您的代码可以正常工作。

Besides, using the path in the UWP may not be a good practice, it does not work for the KnownFolders like the musiclibrary,videolibrary..as well. 此外,在UWP中使用路径可能不是一个好习惯,它对于像音乐库,视频库..这样的KnownFolders也无效。 For more information, please refer to Rob's blog: Skip the path: stick to the StorageFile . 有关更多信息,请参阅Rob的博客: 跳过路径:坚持使用StorageFile

It is recommend to use a Folder​Picker to let the user pick the folder and add it to your app's FutureAccessList or MostRecentlyUsedList , in this way the they can be reloaded later without requiring the user to go through the picker. 建议使用文件夹选择器让用户选择文件夹并将其添加到应用程序的FutureAccessListMostRecentlyUsedList中 ,这样可以在以后重新加载它们而无需用户通过选择器。 For more information, please check: How to track recently-used files and folders . 有关更多信息,请检查: 如何跟踪最近使用的文件和文件夹

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM