我要搜索的文件夹中的所有文件和子文件夹和发现,有具体的细节我怎样才能做到这一点的文件吗? 我用下面的方法,但这种方法,我不得不等待,直到收到的所有文件,然后检查他们的细节与循环,但它确实需要很多的时候,我有超过1000个文件
AllofItems = GetFileList(@"\myfolder").ToArray();
foreach (var item in AllofItems)
{
var file = ShellFile.FromFilePath(item); // for example C:\myfolder\1.jpg
if(file.Properties.System.Title.Value.Equal("Empty")){
coverView.Items.Add(item);
}
}
这是GetFileList
FUNC
public IEnumerable<string> GetFileList(string rootFolderPath)
{
Queue<string> pending = new Queue<string>();
pending.Enqueue(rootFolderPath);
string[] tmp;
while (pending.Count > 0)
{
rootFolderPath = pending.Dequeue();
try
{
tmp = Directory.GetFiles(rootFolderPath);
}
catch (DirectoryNotFoundException) { continue; }
catch (UnauthorizedAccessException)
{
continue;
}
for (int i = 0; i < tmp.Length; i++)
{
yield return tmp[i];
}
tmp = Directory.GetDirectories(rootFolderPath);
for (int i = 0; i < tmp.Length; i++)
{
pending.Enqueue(tmp[i]);
}
}
}
我想在同一时间做这个。检查文件详细搜索时
该DirectoryInfo Class有这个有用的方法。
var dir = new DirectoryInfo(@"C:\myBaseFolder");
FileInfo[] allfiles = dir.GetFiles("*.*", SearchOption.AllDirectories);
这将自动包括所有子目录。
你可以采取这样的await
关键字的优势
private static async Task<FileInfo[]> GetFileList(string rootFolderPath)
{
FileInfo[] allfiles;
await Task.Run(() => {
var dir = new DirectoryInfo(rootFolderPath);
allfiles = dir.GetFiles("*.*", SearchOption.AllDirectories);
});
return allfiles;
}
并调用它像这样
FileInfo[] allFiles = await GetFileList(@"\myfolder");
不要忘记到async
关键字添加到方法做这个调用。