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

后端 未结 7 1791
滥情空心
滥情空心 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:12

    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.

            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.

提交回复
热议问题