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

后端 未结 7 1747
滥情空心
滥情空心 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 07:53

    If you are in a Win32 environment, the most efficient solution could be to use the Win32 API function "WriteFile". There is an example in VB 6, but rewriting it in C# is not difficult.

    0 讨论(0)
  • 2020-11-29 07:58

    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();
    
    0 讨论(0)
  • 2020-11-29 08:05

    I would go this route...

        FileStream fStream = new FileStream("outputpath", FileMode.Append);
        foreach (string item in new string[] { "file1", "file2", "file3" })
            fStream.Write(File.ReadAllBytes(item));
        fStream.Close();
    

    Not file size dependent, no concatenation, and avoiding any possible encoding/decoding issues by reading/writing bytes.

    0 讨论(0)
  • 2020-11-29 08:10

    You mean 3 text files?? Does the result need to be a file again?

    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 :-)

    Cheers

    0 讨论(0)
  • 2020-11-29 08:11
    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);
    
    0 讨论(0)
  • 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.

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