Get all path of certain file extensions in windows 8 store / metro application

477 views Asked by At

THE PROBLEM

I want to get all the path of the files with "custom made" file extension (.pssm and .pnsm) from a folder, and to every of its subfolders (Deep Search) IN A WINDOWS 8 STORE (TABLET) APP.


THE STEPS

  1. Pick a parent folder with a FolderPicker, and save the folder path string

    private async void BrowseButton_Tapped(object sender, TappedRoutedEventArgs e)
    {
        FolderPicker fPicker = new FolderPicker();
    
        fPicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
        fPicker.FileTypeFilter.Add(".pnsm");
        fPicker.FileTypeFilter.Add(".pssm");
    
        StorageFolder sFolder = await fPicker.PickSingleFolderAsync();
    
        if (sFolder != null)
        {
            StorageApplicationPermissions.FutureAccessList.AddOrReplace("PickedFolderToken", sFolder);
            (DataContext as MainPageVM).ParentFolder = sFolder.Path;
        }
    }
    
  2. Search all the file which extension either .pssm or .pnsm under previously saved folder path string and its subfolders.


THE QUESTION

How to do step #2?


ADDITIONAL DETAILS

This is how I do it in my desktop app version (desktop XAML app, not windows 8 one), maybe it can be used as reference (i don't know how to adapt it to win 8 app).

    private async void ButtonRefresh_Click(object sender, RoutedEventArgs e)
    {
        //http://www.codeproject.com/Messages/4762973/Nice-article-and-here-is-Csharp-version-of-code-fr.aspx

        counter = 0;
        string s = (DataContext as MainWindowVM).ParentFolder;
        List<string> exts = (DataContext as MainWindowVM).ExtensionToSearch;

        Action<int> progressTarget = new Action<int>(ReportProgress);
        searchProgress = new Progress<int>(progressTarget);

        List<string> queriedPaths =
            await Task.Run<List<string>>(() => GetAllAccessibleFiles(s, exts, searchProgress));

        (DataContext as MainWindowVM).RefreshList(queriedPaths);
        SaveSearch();

        progressText.Text += "(Search Completed)";
    }

    private List<string> GetAllAccessibleFiles(
                string rootPath, List<string> exts, IProgress<int> searchProgress, List<string> alreadyFound = null)
    {
        if (alreadyFound == null)
        {
            alreadyFound = new List<string>();
        }

        if (searchProgress != null)
        {
            counter++;
            searchProgress.Report(counter);
        }

        DirectoryInfo dI = new DirectoryInfo(rootPath);
        var dirs = dI.EnumerateDirectories().ToList();

        foreach (DirectoryInfo dir in dirs)
        {
            if (!((dir.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden))
            {
                alreadyFound = GetAllAccessibleFiles(dir.FullName, exts, searchProgress, alreadyFound);
            }
        }

        var files = Directory.GetFiles(rootPath);

        foreach (string file in files)
        {
            if (exts.Any(x => file.EndsWith(x)))
            {
                alreadyFound.Add(file);
            }
        }

        return alreadyFound;
    }
1

There are 1 answers

2
Moses Aprico On

It appears that I just a bit near the answer. I have no idea why I didn't cross my mind. So, here's the update to my RefreshButton_Tapped to fetch all the paths.

    private async void RefreshButton_Tapped(object sender, TappedRoutedEventArgs e)
    {
        StorageFolder sFolder = 
            await StorageFolder.GetFolderFromPathAsync((DataContext as MainPageVM).ParentFolder);

        List<string> fileTypeFilter = new List<string>();
        fileTypeFilter.Add(".pnsm");
        fileTypeFilter.Add(".pssm");

        QueryOptions queryOptions =
            new QueryOptions(CommonFileQuery.OrderByName, fileTypeFilter);

        StorageFileQueryResult results = sFolder.CreateFileQueryWithOptions(queryOptions);

        var files = await results.GetFilesAsync();

        List<string> paths = files.Select(x => x.Path.ToString()).ToList();

        SaveSearch();
    }

If anyone have a better answer, feel free to.