What would be the fastest way to concatenate three files in C#?

后端 未结 7 1748
滥情空心
滥情空心 2020-11-29 07:40

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.

Cool = really small code or reall

相关标签:
7条回答
  • 2020-11-29 08:15

    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.

    File.WriteAllLines("out", new[] { "file1", "file2", "file3" }.SelectMany(File.ReadLines));
    

    If your files are binary, See Mehrdad's answer

    0 讨论(0)
提交回复
热议问题