0
的問題使用窗口的某些文件擴展名的所有路徑8店/ Metro應用
我想獲得的所有文件的路徑爲「定製」的文件擴展名(.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的),也許可以作爲參考(我不知道如何適應它贏得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;
}
使用查詢是執行此類搜索的最佳方式;比試圖自己遍歷文件夾結構要容易得多。 – 2014-11-25 15:01:47
@ KraigBrockschmidt-MSFT有時默認迭代器會打「不安全」文件夾並返回錯誤。 – 2014-11-25 16:32:28