C# 从庞大的文件列表中选择前一天的文件
我正在从庞大的文件列表中选择前一天的文件C# 从庞大的文件列表中选择前一天的文件,c#,.net,winforms,performance,optimization,C#,.net,Winforms,Performance,Optimization,我正在从庞大的文件列表中选择前一天的文件 // selecting around 80-120 files from 20,000 - 25,000 FileInfo[] files = (new DirectoryInfo(dirPath)).GetFiles("*.xml"); string[] selectedFiles = (from c in files where c.CreationTime
// selecting around 80-120 files from 20,000 - 25,000
FileInfo[] files = (new DirectoryInfo(dirPath)).GetFiles("*.xml");
string[] selectedFiles = (from c in files
where c.CreationTime >= DateTime.Today.AddDays(-1) && c.CreationTime < DateTime.Today.AddHours(-2.0)
select c.FullName).ToArray();
//从20000-25000个文件中选择大约80-120个文件
FileInfo[]files=(新目录信息(dirPath)).GetFiles(“*.xml”);
字符串[]selectedFiles=(来自文件中的c
其中c.CreationTime>=DateTime.Today.AddDays(-1)和&c.CreationTime
以上程序运行大约需要4-5分钟,您能告诉我如何在不改变功能的情况下优化它吗// file selection is between yesterday 0:00 to yesterday 22:00 <br >
//文件选择在昨天0:00到昨天22:00之间
如上面的代码所示好心的建议 尝试一下:
FileInfo[] files = (new DirectoryInfo(dirPath)).GetFiles("*.xml");
DateTime lowDate = DateTime.Today.AddDays(-1);
DateTime highDate = DateTime.Today.AddHours(-2.0);
string[] selectedFiles = (from c in files
where c.CreationTime >= lowDate && c.CreationTime < highDate
select c.FullName).ToArray();
FileInfo[]files=(新目录信息(dirPath)).GetFiles(“*.xml”);
DateTime lowDate=DateTime.Today.AddDays(-1);
DateTime highDate=DateTime.Today.AddHours(-2.0);
字符串[]selectedFiles=(来自文件中的c
其中c.CreationTime>=lowDate&&c.CreationTime
这些日期可能被计算了20000多次,每次都是如此。如果您只需要知道CreationTime,请不要为每个文件实例化一个新的
FileInfo
类。此外,您不必使用DirectoryInfo
我会用这样的方法:
DateTime lowDate = DateTime.Today.AddDays(-1);
DateTime highDate = DateTime.Today.AddHours(-2.0);
var filteredFileNames = new List<String>();
string[] fileNames;
fileNames = Directory.GetFiles(dirPath, "*.xml")
for (int i = 0; i < fileNames.Length; i++)
{
var creationTime = File.GetCreationTimeUtc(fileNames[i]);
if(creationTime >= lowDate && creationTime < highDate)
{
filteredFileNames.Add(filenNames[i]);
}
}
DateTime lowDate=DateTime.Today.AddDays(-1);
DateTime highDate=DateTime.Today.AddHours(-2.0);
var filteredFileNames=新列表();
字符串[]文件名;
fileNames=Directory.GetFiles(dirPath,*.xml)
for(int i=0;i=lowDate&&creationTime
如果不受I/O限制,您仍然可以将时间范围的一部分划分为不同的任务
/线程
(取决于您使用的.NET版本),并最终累积名称。但是,大部分工作是使用目录.GetFiles
完成的。特别是如果它是一个大目录
当我必须在一个目录中处理大量文件时,我继续使用win32api的FindFirstFile
/FindNextFile
和FindClose
。它提供的开销要少得多,而且速度更快
是否需要以同步方式运行此代码,如果不需要,请尝试异步。您可以在不同的模式下运行此代码thread@CuongLe大多数情况下不会,因为这是某个进程的一部分,除非发生这种情况,否则我无法继续。但是如果有任何多线程的想法,请让我知道