locked
How to get files only for specified extension ? RRS feed

  • General discussion

  • Hi ,

    I am using StorageFolder and StorageFile to read the data

                    StorageFolder mainFolder = await KnownFolders.DocumentsLibrary.CreateFolderAsync("DataFolder", CreationCollisionOption.OpenIfExists);
                    StorageFolder subFolder = await mainFolder.CreateFolderAsync("SubFolder", CreationCollisionOption.OpenIfExists);
                    DataFiles = await subFolder.GetFilesAsync();

    It returns all files available in SubFolder,

    Please let me know how can i get files for specific file extension.


    Thanks,


    C Mahone

    Tuesday, August 6, 2013 11:56 AM

All replies

  • There are limitations on the use of the Documents Library for non-enterprise apps.  Using the file pickers is recommended and supports file type filtering.

    If you do use the Documents Library, you know there is some filtering that takes place in your declarations in the app manifest.

    Beyond that, you can filter the results from your files that are retrieved from your sub-folder like this:

                StorageFolder mainFolder = await KnownFolders.DocumentsLibrary.CreateFolderAsync("DataFolder", CreationCollisionOption.OpenIfExists);
                StorageFolder subFolder = await mainFolder.CreateFolderAsync("SubFolder", CreationCollisionOption.OpenIfExists);
                var DataFiles = await subFolder.GetFilesAsync();
                var FilteredFiles = from f in DataFiles
                                    where f.FileType == ".txt"
                                    select f;
    Now FilteredFiles will be a subset of files from the subFolder which have the ".txt" extension.



    • Edited by jrboddie Tuesday, August 6, 2013 1:09 PM grammar
    Tuesday, August 6, 2013 1:05 PM
  • Hi C Mahone,

    Please open your package.appmanifest and open Capabilities tab, select "Document Library". I am assuming you have done this.

    Now select Declarations tab and add "File Type Associations". Please provide "Display Name" = txt, "Name" = txt and "File type" = .txt considering you want to see txt file only.

    You can also add multiple "File Type Associations according to your need.

    Thanks
    Subhendu


    Tuesday, August 6, 2013 1:07 PM