File.Copy
ユーティリティメソッドを使用するのではなく、ソースファイルをFileStream
として開き、必要な多くの宛先ファイルに対して多くのFileStreams
を開き、ソースから読み取り、各宛先ストリームに書き込むことができます。
更新スループットを向上させるためにParallel.ForEachを使用してファイルを書き込むように変更しました。
public static class FileUtil
{
public static void CopyMultiple(string sourceFilePath, params string[] destinationPaths)
{
if (string.IsNullOrEmpty(sourceFilePath)) throw new ArgumentException("A source file must be specified.", "sourceFilePath");
if (destinationPaths == null || destinationPaths.Length == 0) throw new ArgumentException("At least one destination file must be specified.", "destinationPaths");
Parallel.ForEach(destinationPaths, new ParallelOptions(),
destinationPath =>
{
using (var source = new FileStream(sourceFilePath, FileMode.Open, FileAccess.Read, FileShare.Read))
using (var destination = new FileStream(destinationPath, FileMode.Create))
{
var buffer = new byte[1024];
int read;
while ((read = source.Read(buffer, 0, buffer.Length)) > 0)
{
destination.Write(buffer, 0, read);
}
}
});
}
}
使用法:
FileUtil.CopyMultiple(@"C:\sourceFile1.txt", @"C:\destination1\sourcefile1.txt", @"C:\destination2\sourcefile1.txt");
http://www.java2s.com/Code/CSharp/File-Stream/illustratesuseofMemoryStreams.htm? – Holystream
あなたはそれに問題がありますか、ちょうど科学的であるか、コードを時期尚早に最適化していますか? – ivowiblo
@ivowiblo:私は2時間(10台のコンピュータで15分)かかるプロセスを最適化しようとしています。私は "正常な"コピーより良い方法があると確信しています。 – itsho