以前在Windows窗体应用程序中,this是我为所有文件递归搜索文件夹的方式。我知道Windows应用商店应用程序几乎是沙箱,但必须有一种方法来获取KnownFolder目录中的所有文件。我一直试图用音乐目录来做这件事。但是,它对我不起作用。我已经完成了我的谷歌搜索,我找不到任何线程,说明如何实现这一目标。我尝试了以下代码:
private async void dirScan(string dir)
{
var folDir = await StorageFolder.GetFolderFromPathAsync(dir);
foreach (var d in await folDir.GetFoldersAsync())
{
foreach(var f in await d.GetFilesAsync())
{
knownMusicDir.Add(f.Path.ToString());
}
dirScan(d.ToString());
}
}
我希望有人可以查看我的代码,并希望能够纠正它。提前谢谢!
答案 0 :(得分:4)
您可以使用此扩展方法:
public static async Task<IEnumerable<StorageFile>> GetAllFilesAsync(this StorageFolder folder)
{
IEnumerable<StorageFile> files = await folder.GetFilesAsync();
IEnumerable<StorageFolder> folders = await folder.GetFoldersAsync();
foreach (StorageFolder subfolder in folders)
files = files.Concat(await subfolder.GetAllFilesAsync());
return files;
}
答案 1 :(得分:3)
这适用于我KnownFolders
:
ObservableCollection<string> files;
public MainPage()
{
this.InitializeComponent();
files = new ObservableCollection<string>();
}
private async void GetFiles(StorageFolder folder)
{
StorageFolder fold = folder;
var items = await fold.GetItemsAsync();
foreach (var item in items)
{
if (item.GetType() == typeof(StorageFile))
files.Add(item.Path.ToString());
else
GetFiles(item as StorageFolder);
}
listView.ItemsSource = files;
}