问题
我想从一个文件夹中获取具有“自定义”文件扩展名(.pssm 和 .pnsm)的文件的所有路径,并在 WINDOWS 8 STORE(平板电脑)应用程序中获取其每个子文件夹(深度搜索)。
步骤
选择一个带有 的父文件夹
FolderPicker
,并保存文件夹路径字符串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; } }
在先前保存的文件夹路径字符串及其子文件夹下搜索所有扩展名为 .pssm 或 .pnsm 的文件。
问题
如何进行第 2 步?
额外细节
这就是我在我的桌面应用程序版本(桌面 XAML 应用程序,而不是 windows 8 之一)中的做法,也许它可以用作参考(我不知道如何将其调整为 win 8 应用程序)。
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;
}