This question already has an answer here:
这个问题在这里已有答案:
- Getting files by creation date in .NET 7 answers
在.NET 7答案中按创建日期获取文件
Is there a way to get all the files in the directory and subdirectories after a date range?
有没有办法在日期范围之后获取目录和子目录中的所有文件?
I know that Directory.GetFiles()
can iterate all the folders however this does not give me access to a file object in which I can do an if statement on.
我知道Directory.GetFiles()可以迭代所有文件夹但是这不能让我访问一个文件对象,我可以在其中执行if语句。
1 个解决方案
#1
You could use File.GetCreationTime
and SearchOption.AllDirectories
, for example:
您可以使用File.GetCreationTime和SearchOption.AllDirectories,例如:
var allFilesIn2015 = Directory.EnumerateFiles("Dir-Path", "*.*", System.IO.SearchOption.AllDirectories)
.Where(path => File.GetCreationTime(path).Year == 2015)
.ToList();
You could also use FileInfo
s and it's properties CreationTime
or LastAccessTime
:
您还可以使用FileInfos及其属性CreationTime或LastAccessTime:
DirectoryInfo dir = new DirectoryInfo("Dir-Path");
var allFileInfosIn2015 = dir.EnumerateFiles("*.*", System.IO.SearchOption.AllDirectories)
.Where(fi => fi.CreationTime.Year == 2015)
.ToList();
I use EnumerateFiles
since that is more efficient in this case. It does not need to load all files into memory before it can start processing them.
我使用EnumerateFiles,因为在这种情况下更有效。在开始处理之前,不需要将所有文件加载到内存中。
Quote:
The EnumerateFiles and GetFiles methods differ as follows: When you use EnumerateFiles, you can start enumerating the collection of names before the whole collection is returned; when you use GetFiles, you must wait for the whole array of names to be returned before you can access the array. Therefore, when you are working with many files and directories, EnumerateFiles can be more efficient.
EnumerateFiles和GetFiles方法的不同之处如下:当您使用EnumerateFiles时,您可以在返回整个集合之前开始枚举名称集合;当您使用GetFiles时,您必须等待返回整个名称数组,然后才能访问该数组。因此,当您使用许多文件和目录时,EnumerateFiles可以更高效。
#1
You could use File.GetCreationTime
and SearchOption.AllDirectories
, for example:
您可以使用File.GetCreationTime和SearchOption.AllDirectories,例如:
var allFilesIn2015 = Directory.EnumerateFiles("Dir-Path", "*.*", System.IO.SearchOption.AllDirectories)
.Where(path => File.GetCreationTime(path).Year == 2015)
.ToList();
You could also use FileInfo
s and it's properties CreationTime
or LastAccessTime
:
您还可以使用FileInfos及其属性CreationTime或LastAccessTime:
DirectoryInfo dir = new DirectoryInfo("Dir-Path");
var allFileInfosIn2015 = dir.EnumerateFiles("*.*", System.IO.SearchOption.AllDirectories)
.Where(fi => fi.CreationTime.Year == 2015)
.ToList();
I use EnumerateFiles
since that is more efficient in this case. It does not need to load all files into memory before it can start processing them.
我使用EnumerateFiles,因为在这种情况下更有效。在开始处理之前,不需要将所有文件加载到内存中。
Quote:
The EnumerateFiles and GetFiles methods differ as follows: When you use EnumerateFiles, you can start enumerating the collection of names before the whole collection is returned; when you use GetFiles, you must wait for the whole array of names to be returned before you can access the array. Therefore, when you are working with many files and directories, EnumerateFiles can be more efficient.
EnumerateFiles和GetFiles方法的不同之处如下:当您使用EnumerateFiles时,您可以在返回整个集合之前开始枚举名称集合;当您使用GetFiles时,您必须等待返回整个名称数组,然后才能访问该数组。因此,当您使用许多文件和目录时,EnumerateFiles可以更高效。