从一个目录复制到另一个文件最快的方法 [英] Fastest way to copy files from one directory to another
本文介绍了从一个目录复制到另一个文件最快的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要从一个目录复制到另一个,这取决于在SQL数据库的表的文件名是否存在。
I need to copy files from one directory to another, depending on the existence of the file name in a table of SQL database.
有关此我使用下面的代码:
For this I use the following code:
using(SqlConnection connection = new SqlConnection("datasource or route"))
{
connection.Open();
using(SqlCommand cmd = new SqlCommand("SELECT idPic, namePicFile FROM DocPicFiles", connection))
using (SqlDataReader reader = cmd.ExecuteReader())
{
if (reader != null)
{
while (reader.Read())
{
//picList IS AN ARRAY THAT Contains All the files names in a directory
if (picList.Any(s => s.Contains(reader["namePicFile"].ToString())))
{
File.Copy("theFile in the Directory or array picList", "the destiny directory"+ ".jpg", false)
}
}
}
}
}
有没有什么办法,这能够在更短的时间内完成?它需要为做到这一点,为20.876的记录。
Is there any way that this can be done in less time? It takes 1 hour for do that, for 20.876 records.
推荐答案
由于您的I / O子系统几乎可以肯定是botteneck这里1个小时,使用并行任务库大概是尽善尽美:
Since your i/o subsystem is almost certainly the botteneck here, using the parallel task library is probably about as good as it gets:
static void Main(string[] args)
{
DirectoryInfo source = new DirectoryInfo( args[0] ) ;
DirectoryInfo destination = new DirectoryInfo( args[1] ) ;
HashSet<string> filesToBeCopied = new HashSet<string>( ReadFileNamesFromDatabase() , StringComparer.OrdinalIgnoreCase ) ;
// you'll probably have to play with MaxDegreeOfParallellism so as to avoid swamping the i/o system
ParallelOptions options= new ParallelOptions { MaxDegreeOfParallelism = 4 } ;
Parallel.ForEach( filesToBeCopied.SelectMany( fn => source.EnumerateFiles( fn ) ) , options , fi => {
string destinationPath = Path.Combine( destination.FullName , Path.ChangeExtension( fi.Name , ".jpg") ) ;
fi.CopyTo( destinationPath , false ) ;
}) ;
}
public static IEnumerable<string> ReadFileNamesFromDatabase()
{
using ( SqlConnection connection = new SqlConnection( "connection-string" ) )
using ( SqlCommand cmd = connection.CreateCommand() )
{
cmd.CommandType = CommandType.Text ;
cmd.CommandText = @"
select idPic ,
namePicFile
from DocPicFiles
" ;
connection.Open() ;
using ( SqlDataReader reader = cmd.ExecuteReader() )
{
while ( reader.Read() )
{
yield return reader.GetString(1) ;
}
}
connection.Close() ;
}
}
这篇关于从一个目录复制到另一个文件最快的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文