复制目录的性能 [英] performance of copying directories
问题描述
我使用C#从一个目录中的文件复制到另一个目录。
我使用的代码从MSDN但它很慢服用一分钟左右,以
复制了几个演出的。它只需几秒钟的探险家。
http://channel9.msdn.com /论坛/ TechOff / 257490-如何-复制的目录式-C
切切实实的有一个更快的方法..:)
私有静态无效复制(字符串sourceDirectory,串targetdirectory中)
{
DirectoryInfo的diSource =新DirectoryInfo的(sourceDirectory);
DirectoryInfo的diTarget =新DirectoryInfo的(targetdirectory中);
CopyAll(diSource,diTarget);
}
私有静态无效CopyAll(DirectoryInfo的来源,DirectoryInfo的目标)
{
//检查目标目录是否存在,如果没有,创建它。
如果(Directory.Exists(target.FullName)==假)
{
Directory.CreateDirectory(target.FullName);
}
//复制每个文件到它的新目录中。
的foreach(FileInfo的网络连接source.GetFiles())
{
fi.CopyTo(Path.Combine(target.ToString(),fi.Name),TRUE);
}
//复制每个子目录使用递归。
的foreach(DirectoryInfo的diSourceSubDir在source.GetDirectories())
{
DirectoryInfo的nextTargetSubDir =
target.CreateSubdirectory(diSourceSubDir.Name);
CopyAll(diSourceSubDir,nextTargetSubDir);
}
}
使用并行我能够更快的复制6gigs一分钟内比探险家和XCOPY。
私有静态无效CopyAll(字符串SOURCEPATH,字符串的DestinationPath)
{
的String [] =目录System.IO.Directory.GetDirectories(SOURCEPATH,SearchOption.AllDirectories*。*);
Parallel.ForEach(目录,dirPath =>
{
Directory.CreateDirectory(dirPath.Replace(SOURCEPATH,的DestinationPath));
});
的String []文件= System.IO.Directory.GetFiles(SOURCEPATH,SearchOption.AllDirectories*。*);
Parallel.ForEach(文件的新路径= GT;
{
File.Copy(的新路径,newPath.Replace(SOURCEPATH,的DestinationPath));
});
}
u的使用什么是递归。它总是减慢系统。
使用这个,因为它没有递归。
无效CopyAll(字符串SOURCEPATH,字符串的DestinationPath)
{
//现在创建的所有目录
的foreach(字符串dirPath的Directory.GetDirectories(SOURCEPATH,*。*,
SearchOption.AllDirectories))
Directory.CreateDirectory(dirPath.Replace(SOURCEPATH,的DestinationPath));
//将所有文件复制
的foreach(字符串的新路径在Directory.GetFiles(SOURCEPATH,*。*,
SearchOption.AllDirectories))
档。复制(的新路径,newPath.Replace(SOURCEPATH,的DestinationPath));
}
I am using C# to copy files from one directory to another directory. I am using code from msdn but its pretty slow taking a minute or so to copy a couple of gigs. It only takes seconds in explorer. http://channel9.msdn.com/Forums/TechOff/257490-How-Copy-directories-in-C Surly there a faster way..:)
private static void Copy(string sourceDirectory, string targetDirectory)
{
DirectoryInfo diSource = new DirectoryInfo(sourceDirectory);
DirectoryInfo diTarget = new DirectoryInfo(targetDirectory);
CopyAll(diSource, diTarget);
}
private static void CopyAll(DirectoryInfo source, DirectoryInfo target)
{
// Check if the target directory exists, if not, create it.
if (Directory.Exists(target.FullName) == false)
{
Directory.CreateDirectory(target.FullName);
}
// Copy each file into it's new directory.
foreach (FileInfo fi in source.GetFiles())
{
fi.CopyTo(Path.Combine(target.ToString(), fi.Name), true);
}
// Copy each subdirectory using recursion.
foreach (DirectoryInfo diSourceSubDir in source.GetDirectories())
{
DirectoryInfo nextTargetSubDir =
target.CreateSubdirectory(diSourceSubDir.Name);
CopyAll(diSourceSubDir, nextTargetSubDir);
}
}
Using Parallel I was able to copy 6gigs in under a minute faster than explorer and xcopy.
private static void CopyAll(string SourcePath, string DestinationPath)
{
string[] directories = System.IO.Directory.GetDirectories(SourcePath, "*.*", SearchOption.AllDirectories);
Parallel.ForEach(directories, dirPath =>
{
Directory.CreateDirectory(dirPath.Replace(SourcePath, DestinationPath));
});
string[] files = System.IO.Directory.GetFiles(SourcePath, "*.*", SearchOption.AllDirectories);
Parallel.ForEach(files, newPath =>
{
File.Copy(newPath, newPath.Replace(SourcePath, DestinationPath));
});
}
What u are using is recursion. It always slows the system.
Use this as it has no recursion.
void CopyAll (string SourcePath, string DestinationPath)
{
//Now Create all of the directories
foreach (string dirPath in Directory.GetDirectories(SourcePath, "*.*",
SearchOption.AllDirectories))
Directory.CreateDirectory(dirPath.Replace(SourcePath, DestinationPath));
//Copy all the files
foreach (string newPath in Directory.GetFiles(SourcePath, "*.*",
SearchOption.AllDirectories))
File.Copy(newPath, newPath.Replace(SourcePath, DestinationPath));
}
这篇关于复制目录的性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!