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
Pick a parent folder with a
FolderPicker
, and save the folder path stringprivate 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; } }
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;
}
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.If anyone have a better answer, feel free to.