在C#中高性能文件复制? [英] Performant File Copy in C#?

查看:390
本文介绍了在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/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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆