在 C# 中连接三个文件的最快方法是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/444309/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What would be the fastest way to concatenate three files in C#?
提问by Bobby Bruckovnic
I need to concatenate 3 files using C#. A header file, content, and a footer file, but I want to do this as cool as it can be done.
我需要使用 C# 连接 3 个文件。一个头文件、一个内容和一个页脚文件,但我想尽可能地做到这一点。
Cool = really small code or really fast (non-assembly code).
Cool = 非常小的代码或非常快(非汇编代码)。
采纳答案by Mehrdad Afshari
void CopyStream(Stream destination, Stream source) {
int count;
byte[] buffer = new byte[BUFFER_SIZE];
while( (count = source.Read(buffer, 0, buffer.Length)) > 0)
destination.Write(buffer, 0, count);
}
CopyStream(outputFileStream, fileStream1);
CopyStream(outputFileStream, fileStream2);
CopyStream(outputFileStream, fileStream3);
回答by Jimmy
If your files are text and not large, there's something to be said for dead-simple, obvious code. I'd use the following.
如果您的文件是文本文件并且不是很大,那么对于非常简单、明显的代码来说,有些话要说。我会使用以下内容。
File.ReadAllText("file1") + File.ReadAllText("file2") + File.ReadAllText("file3");
If your files are large text files and you're on Framework 4.0, you can use File.ReadLines
to avoid buffering the entire file.
如果您的文件是大型文本文件并且您使用的是 Framework 4.0,则可以使用File.ReadLines
避免缓冲整个文件。
File.WriteAllLines("out", new[] { "file1", "file2", "file3" }.SelectMany(File.ReadLines));
If your files are binary, See Mehrdad's answer
如果您的文件是二进制文件,请参阅Mehrdad 的回答
回答by TcKs
回答by Kev
Another way....how about letting the OS do it for you?:
另一种方式......让操作系统为你做这件事怎么样?:
ProcessStartInfo psi = new ProcessStartInfo("cmd.exe",
String.Format(@" /c copy {0} + {1} + {2} {3}",
file1, file2, file3, dest));
psi.UseShellExecute = false;
Process process = Process.Start(psi);
process.WaitForExit();
回答by marc_s
You mean 3 text files?? Does the result need to be a file again?
你的意思是3个文本文件??结果是否需要再次成为文件?
How about something like:
怎么样:
string contents1 = File.ReadAllText(filename1);
string contents2 = File.ReadAllText(filename2);
string contents3 = File.ReadAllText(filename3);
File.WriteAllText(outputFileName, contents1 + contents2 + contents3);
Of course, with a StringBuilder and a bit of extra smarts, you could easily extend that to handle any number of input files :-)
当然,使用 StringBuilder 和一些额外的智能,您可以轻松扩展它以处理任意数量的输入文件:-)
Cheers
干杯
回答by user664769
I support Mehrdad Afshari on his code being exactly same as used in System.IO.Stream.CopyTo. I would still wonder why did he not use that same function instead of rewriting its implementation.
我支持 Mehrdad Afshari 的代码与System.IO.Stream.CopyTo 中使用的完全相同。我仍然想知道为什么他不使用相同的函数而不是重写它的实现。
string[] srcFileNames = { "file1.txt", "file2.txt", "file3.txt" };
string destFileName = "destFile.txt";
using (Stream destStream = File.OpenWrite(destFileName))
{
foreach (string srcFileName in srcFileNames)
{
using (Stream srcStream = File.OpenRead(srcFileName))
{
srcStream.CopyTo(destStream);
}
}
}
According to the disassembler (ILSpy) the default buffer size is 4096. CopyTo function has got an overload, which lets you specify the buffer size in case you are not happy with 4096 bytes.
根据反汇编程序 ( ILSpy),默认缓冲区大小为 4096。 CopyTo 函数有一个重载,它允许您指定缓冲区大小,以防您对 4096 字节不满意。