我正在寻找一种方法,该方法将采用文件扩展名类型和目录,并返回按最新创建日期排序的该目录和子目录中的所有文件,即首先是最新文件。
到目前为止,我已经确定了以下方法,该方法应该很快,但是有没有更好的方法可以做到这一点,我需要它返回 FileInfo 而不是字符串并按上述顺序排序。
public static IEnumerable<string> GetFileList(string fileSearchPattern, string rootFolderPath)
{
Queue<string> pending = new Queue<string>();
pending.Enqueue(rootFolderPath);
string[] tmp;
while (pending.Count > 0)
{
rootFolderPath = pending.Dequeue();
tmp = Directory.GetFiles(rootFolderPath, fileSearchPattern);
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]);
}
}
}
最佳答案
当我研究这个问题空间时,我发现没有一种快速
方法可以做到这一点。原因是无论您采取什么方法,您最终都必须转到操作系统来获取目录中的文件列表。并且文件系统不会像搜索引擎那样缓存/索引。所以你最终需要自己重新抓取文件系统。
但是,一旦获得原始信息,您就可以自己为其编制索引。
关于c# - 从目录和子目录中获取文件,按最新创建日期快速排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25448730/