在C#中高性能文件复制? [英] Performant File Copy in C#?
问题描述
我有500K左右的JPG文件一个巨大的目录,我想归档早于特定日期之前的所有文件。目前,该脚本需要时间来运行。
I have a huge directory of about 500k jpg files, and I'd like to archive all files that are older than a certain date. Currently, the script takes hours to run.
这有很多事情要做GoGrid的的存储服务器的非常小便表现不佳,但在同一时间,我敢肯定有一种方法更有效的方式拉姆/ CPU智慧来完成我在做什么。
This has a lot to do with the very piss-poor performance of GoGrid's storage servers, but at the same time, I'm sure there's a way more efficient way Ram/Cpu wise to accomplish what I'm doing.
下面是我的代码有:
var dirInfo = new DirectoryInfo(PathToSource);
var fileInfo = dirInfo.GetFiles("*.*");
var filesToArchive = fileInfo.Where(f =>
f.LastWriteTime.Date < StartThresholdInDays.Days().Ago().Date
&& f.LastWriteTime.Date >= StopThresholdInDays.Days().Ago().Date
);
foreach (var file in filesToArchive)
{
file.CopyTo(PathToTarget+file.Name);
}
的天数()。前()的东西就是语法糖。
The Days().Ago() stuff is just syntactic sugar.
推荐答案
虽然.NET 4.0提供了懒 Directory.EnumerateFiles
,你可以做这个权利现在.NET 3.5:
While .NET 4.0 provides the lazy Directory.EnumerateFiles
, you can do this right now on .NET 3.5:
- 的 http://weblogs.asp.net/podwysocki/archive/2008/10 /16/functional-net-fighting-friction-in-the-bcl-with-directory-getfiles.aspx
- 的 http://codebetter.com/blogs/matthew.podwysocki/archive/ 2009年/ 03/18 /功能性净懒惰,变得-you.aspx
- 的 http://bugsquash.blogspot.com/2008/07/y-combinator-and-linq.html
- http://www.atalasoft.com/cs/blogs/stevehawley/archive/2009/06/02/more-ienumerable-t-fun.aspx
- http://weblogs.asp.net/podwysocki/archive/2008/10/16/functional-net-fighting-friction-in-the-bcl-with-directory-getfiles.aspx
- http://codebetter.com/blogs/matthew.podwysocki/archive/2009/03/18/functional-net-laziness-becomes-you.aspx
- http://bugsquash.blogspot.com/2008/07/y-combinator-and-linq.html
- http://www.atalasoft.com/cs/blogs/stevehawley/archive/2009/06/02/more-ienumerable-t-fun.aspx
这篇关于在C#中高性能文件复制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!