locked
how to read multiple files of a specified format from a selected folder?

    Question

  • i want to read multiple files of specified format from a selected folder, even if the selected folder contains multiple sub-folders, is it possible, if so please provide me a sample.
    Thursday, January 8, 2015 6:48 AM

Answers

  • in winrt you can use this

    var folderPicker = new FolderPicker();
                var folder = await folderPicker.PickSingleFolderAsync()
                var queryOptions = new QueryOptions(CommonFileQuery.DefaultQuery, new[] { ".jpg" });
                queryOptions.FolderDepth = FolderDepth.Deep;
    
                var query = folder.CreateItemQueryWithOptions(queryOptions);
                var items = await query.GetItemsAsync();
                var files = items.OfType<IStorageFile>().ToList();


    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    • Marked as answer by kranthi88 Friday, January 9, 2015 1:59 PM
    Thursday, January 8, 2015 1:48 PM

All replies

  • This loop will allow you to browser all directories within a specific root:

     foreach (string dirPath in Directory.GetDirectories(WriteYourPathHere, "*",SearchOption.AllDirectories))

    //do your work here

    What do you mean by files of specified format? do you mean the file type?


    Fouad Romieh

    Thursday, January 8, 2015 7:26 AM
  • by specified file format i mean .txt, .mp3 etc----, when i select a folder(if that folder contains sub folders then the files in those sub folders too) all the files of that specified format must be retrieved to a list view.
    Thursday, January 8, 2015 11:51 AM
  • in winrt you can use this

    var folderPicker = new FolderPicker();
                var folder = await folderPicker.PickSingleFolderAsync()
                var queryOptions = new QueryOptions(CommonFileQuery.DefaultQuery, new[] { ".jpg" });
                queryOptions.FolderDepth = FolderDepth.Deep;
    
                var query = folder.CreateItemQueryWithOptions(queryOptions);
                var items = await query.GetItemsAsync();
                var files = items.OfType<IStorageFile>().ToList();


    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    • Marked as answer by kranthi88 Friday, January 9, 2015 1:59 PM
    Thursday, January 8, 2015 1:48 PM
  • try this:

    foreach (string dirPath in Directory.GetDirectories(WriteYourPathHere, "*",SearchOption.AllDirectories))
    string[] files = System.IO.Directory.GetFiles(dirPath, "*.txt");

    //loop over files array and do what you want with .txt files picked up during the loop

    OR in one line also:

    foreach (string file in Directory.GetFiles(WriteYourPathHere, "*.txt", SearchOption.AllDirectories))


    Fouad Roumieh




    Friday, January 9, 2015 9:52 AM
  • Store apps can't use the System.IO namespace.

    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    Friday, January 9, 2015 12:10 PM
  • thanx a lot dave, your code is working flawlessly!!
    Friday, January 9, 2015 1:59 PM