Search code examples
c#filestreamfilestream

FileStream move instead of copy


I'm trying to concatanate two very big files (potentially, a few GB). I have no need to preserve their original content, I'm only interested in the result.

I've seen that the most efficient way to append one file to the other is using FileStream.CopyTo().

However, as you all know a move operation is much cheaper than a copy. If I wanted to move the files around the file system, I would use File.Move and not File.Copy.

Is there any equivalent to file streams? Or any other way surrounding the file? I can also use non-C# methods.


Solution

  • No.

    The file-system move operation only changes the path to a file - that's what makes it fast. There's no analogue when you're trying to merge two files, no matter how.

    The best you can do is append the contents of the second file to the first file - at least you'll avoid copying the first file. That's what FileStream.CopyTo does (don't forget to seek to the end of the first file first).