我有这个功能,我用来读取目录并获取具有特定搜索模式的文件.有没有办法根据创建日期或修改日期使用搜索模式?
public static List<FileInfo> GetFileList(string fileSearchPattern, string rootFolderPath)
{
DirectoryInfo rootDir = new DirectoryInfo(rootFolderPath);
List<DirectoryInfo> dirList = new List<DirectoryInfo>(
rootDir.GetDirectories("*", SearchOption.AllDirectories));
dirList.Add(rootDir);
List<FileInfo> fileList = new List<FileInfo>();
foreach (DirectoryInfo dir in dirList)
{
fileList.AddRange(
dir.GetFiles(fileSearchPattern, SearchOption.TopDirectoryOnly));
}
return fileList;
}
Run Code Online (Sandbox Code Playgroud)
Ica*_*rus 15
不,但你可以用Linq快速过滤它们; 就像是:
var files = from c in directoryInfo.GetFiles()
where c.CreationTime >somedate
select c;
Run Code Online (Sandbox Code Playgroud)
这给了我上个月的文件,例如:
new DirectoryInfo("c:\\Aaron")
.EnumerateFileSystemInfos("*.*", SearchOption.AllDirectories)
.Where(file =>
file.CreationTime > DateTime.Now.AddMonths(-1));
Run Code Online (Sandbox Code Playgroud)