C#中的UWP递归文件搜索非常慢 [英] UWP Recursive File Search in C# is awfully slow

查看:114
本文介绍了C#中的UWP递归文件搜索非常慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码来递归搜索所选文件夹中的图像.该文件夹中有超过120000张图像(是的,我是摄影师!),而且速度非常慢,例如我必须在10分钟后将其停止,但尚未完成.相比之下,我的Python代码(解释了!)在不到2分钟的时间内完成了相同的工作.

I am using the following code to search recursively the images in a selected folder. I have over 120000 images in that folder (Yes, I'm a photographer!) and it is incredibly slow, for e.g. I have to stop it after 10 minutes and it's not done yet. In comparison, my Python code (interpreted!) does the same in less than 2 minutes.

有什么方法可以使此代码更高效?它工作正常,没关系,但速度非常慢...

Is there any way to make this code more efficient? It works fine, that's ok, but only very slowly...

    public List<StorageFile> _allFiles;
    public List<StorageFile> ShuffledFiles;
    public int i = 0;
    public int ri = 0;
    public Boolean random = false;
    public int numfiles = 0;

    //Get the starting folder for recursive search
    private static async Task<StorageFolder> SelectFolderAsync()
    {
        var folderPicker = new Windows.Storage.Pickers.FolderPicker
        {
            SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.Desktop
        };
        //Selects the folder with a FolderPicker and returns the selected StorageFolder
        folderPicker.FileTypeFilter.Add("*");
        StorageFolder folder = await folderPicker.PickSingleFolderAsync();
        return folder;
    }

    //Get the list of files recursively
    private async Task GetFilesInFolder(StorageFolder folder)
    {
        var items = await folder.GetItemsAsync();
        foreach (var item in items)
        {
            //If it's a folder, read each file in it and add them to the list of files "_allFiles"
            if (item is StorageFile )
            {
                StorageFile typetest = item as StorageFile;
                String ext = typetest.FileType.ToLower();
                if ((ext == ".jpg") || (ext == ".jpeg") || (ext == ".tiff") || (ext == ".cr2") || (ext == ".nef") || (ext == ".bmp") || (ext == ".png"))
                { _allFiles.Add(item as StorageFile);
                    numfiles = numfiles + 1;
                    //Display the file count so I can track where it's at...
                    cmdbar.Content = "Number of slides:"+numfiles.ToString();
                }
            }
            else
            //otherwise, recursively search the folder
                await GetFilesInFolder(item as StorageFolder);
        }
    }


    //Select the directory, load the files and display the first file
    private async void LoadMediaFile(object sender, TappedRoutedEventArgs e)
    {
        StorageFolder root = await SelectFolderAsync();
        //Initialises the file list _allFiles, the filecount numfiles, and the pointers to the list i and ri
        _allFiles = new List<StorageFile>();
        numfiles = 0;
        //Reads the files recursively into the list
        await GetFilesInFolder(root);
    }

推荐答案

我没有太多照片可以快速测试,但是您可以尝试两件事.

I don't have so many photo that I quickly can test but two things you can try.

  1. 使用System.IO命名空间;当我切换到该API时,我注意到我的应用程序中有一些改进.

  1. Use the System.IO namespace; I noticed some improvements improvements in my app when I switched to that API.

不要通过尝试使用seaerch api手动对其进行迭代:

Don't manually iterate over it by try to use the seaerch api: https://docs.microsoft.com/en-us/windows/uwp/files/quickstart-listing-files-and-folders#query-files-in-a-location-and-enumerate-matching-files (I think would be the best approach)

这篇关于C#中的UWP递归文件搜索非常慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆