locked
enumeration of files and folders RRS feed

  • Question

  • Hi All,

    I want to enumerate files/folders other than the Knownfolders. I have chosen the folder to enumerate using the Folder Picker.

    I would like to know if there is an API which will give me all the files in that folder recursively. 

    I will let you know what I have tried, FYI I am choosing "C:\" from the Folder picker:

    •   I wrote a recursive async function to parse every folder for the files. Although the function being async , it blocks the UI and is taking lot of time.
    • I tried the Queryoptions to get the files recursively(as provided in the Folder enumeration sample) but that did not return me anything. It returned "0" files.

    Please help me with this.



    recieved alert

    Friday, October 11, 2013 6:58 AM

Answers

  • i have this code:

    async void ItemView_ItemClick(object sender, ItemClickEventArgs e)
            {
                var folderPicker = new FolderPicker();
                folderPicker.FileTypeFilter.Add(".doc");
                var folder = await folderPicker.PickSingleFolderAsync();
    
                var folders = await GetFoldersAndFiles(folder);
    
                var messageDialog = new MessageDialog("Complete");
                await messageDialog.ShowAsync();
            }
    
            private async System.Threading.Tasks.Task<List<IStorageItem>> GetFoldersAndFiles(Windows.Storage.StorageFolder folder)
            {
                List<IStorageItem> item = new List<IStorageItem>();
                
                foreach (var subFolder in await folder.GetFoldersAsync())
                {
                    item.Add(subFolder);
                    item.AddRange(await GetFoldersAndFiles(subFolder));
                }
    
                foreach (var subFolder in await folder.GetFilesAsync())
                {
                    item.Add(subFolder);
                }
                return item;
            }

    doesnt hang up my ui. keeps reponsiove


    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    • Marked as answer by padaka Tuesday, October 15, 2013 6:14 AM
    Saturday, October 12, 2013 11:10 AM

All replies

  • Can you show your code? A folder query as demonstrated in the sample is the right way to do this and should work.

    You should be able to use the same code as Quickstart: Accessing files programmatically except with the StorageFolder from the picker instead of the PicturesLibrary.

    Friday, October 11, 2013 3:00 PM
    Moderator
  • i have this code:

    async void ItemView_ItemClick(object sender, ItemClickEventArgs e)
            {
                var folderPicker = new FolderPicker();
                folderPicker.FileTypeFilter.Add(".doc");
                var folder = await folderPicker.PickSingleFolderAsync();
    
                var folders = await GetFoldersAndFiles(folder);
    
                var messageDialog = new MessageDialog("Complete");
                await messageDialog.ShowAsync();
            }
    
            private async System.Threading.Tasks.Task<List<IStorageItem>> GetFoldersAndFiles(Windows.Storage.StorageFolder folder)
            {
                List<IStorageItem> item = new List<IStorageItem>();
                
                foreach (var subFolder in await folder.GetFoldersAsync())
                {
                    item.Add(subFolder);
                    item.AddRange(await GetFoldersAndFiles(subFolder));
                }
    
                foreach (var subFolder in await folder.GetFilesAsync())
                {
                    item.Add(subFolder);
                }
                return item;
            }

    doesnt hang up my ui. keeps reponsiove


    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    • Marked as answer by padaka Tuesday, October 15, 2013 6:14 AM
    Saturday, October 12, 2013 11:10 AM
  • Thanks for the answer. This doesnt hang the UI but the app crashes giving the following exception:

    An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in mscorlib.dll

    Additional information: Error HRESULT E_FAIL has been returned from a call to a COM component.

    I am doubting this is because of Stack overflow, could you please shed light on this.

    Thanks.


    recieved alert

    Tuesday, October 15, 2013 6:17 AM
  • Thanks for share. This is very helpful with me


    • Edited by tran nguyen Tuesday, October 15, 2013 6:25 AM fix
    Tuesday, October 15, 2013 6:20 AM