如何将一个文件同时复制到多个位置 [英] How to copy one file to many locations simultaneously
问题描述
我想找到一种方法将一个文件复制到多个位置同时(使用C#)。
表示我不只需要一次读取原始文件,并将文件粘贴到另一个位置(在本地网络上)。
据我的测试显示,
File.Copy()
会再次读取源文件。
据我所知,即使在使用内存时,内存块也会被锁定。 / p>
基本上,我想模仿复制粘贴到一个复制的形式,多个粘贴,没有再次从硬盘读取。
为什么?
因为最终,我需要复制一个文件夹(超过1GB)到许多计算机,瓶颈是我需要阅读源文件的部分。
<所以,
是可以实现的吗?
而不是使用 更新将其更改为c $ c>,然后将所需的目标文件从源代码读取,然后写入每个目标流。使用Parallel.ForEach编写文件来提高吞吐量。 I want to find a way to copy one file to multiple locations simultaneously (with C#). means that i don't want the original file to be read only one time, and to "paste" the file to another locations (on local network). as far as my tests showed me, the will always read the source again. and as far as i understand, even while using memory, that memory piece gets locked. so basically, i want to mimic the "copy-paste" to the form of one "copy", and multiple "paste", without re-reading from the Hard Drive again. Why ?
because eventually, i need to copy one folder (more than 1GB) to many computers, and the bottleneck is the part the i need to read the source file. So,
Is it even possible to achieve ? Rather than using the UPDATE Changed it to write files using Parallel.ForEach to improve throughput. Usage:
这篇关于如何将一个文件同时复制到多个位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! File.Copy
实用程序方法,您可以打开源文件作为 FileStream
,然后打开多个 FileStreams $
pre $ public c ++ public static class FileUtil
{
public static void CopyMultiple(string sourceFilePath,params string [] destinationPaths)
{
if(string.IsNullOrEmpty(sourceFilePath))throw new ArgumentException(必须指定源文件,sourceFilePath);
if(destinationPaths == null || destinationPaths.Length == 0)抛出新的ArgumentException(至少有一个目标文件必须被指定,destinationPaths);
使用(var source = new FileStream(sourceFilePath,FileMode.Open,)创建一个新的Parallel.ForEach(destinationPaths,新的ParallelOptions(),
destinationPath =>
{ 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,阅读);
}
}
});
$ / code $ / pre
用法:
FileUtil.CopyMultiple(@C:\ sourceFile1.txt,@C:\destination1\sourcefile1.txt,@C:\ destination2\sourcefile1.txt);
File.Copy()
File.Copy
utility method, you could open the source file as a FileStream
, then open as many FileStreams
to however many destination files you need, read from the source, and write to each destination stream.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");