I need to directly append one file to the end of the other, but i dont want to load the entire file into memory, and using a buffer seams to be much slower than a direct approach... (the actual bytes are to be appended, not text files...)
Asked
Active
Viewed 2,625 times
1 Answers
1
You can use FileMode.Append
:
Using reader = File.OpenRead(pathRead)
Using writer = New FileStream(pathWrite, FileMode.Append)
Dim b = reader.ReadByte()
While b <> -1
writer.WriteByte(CByte(b))
b = reader.ReadByte()
End While
End Using
End Using
Append Opens the file if it exists and seeks to the end of the file, or creates a new file. This requires FileIOPermissionAccess.Append permission. FileMode.Append can be used only in conjunction with FileAccess.Write. Trying to seek to a position before the end of the file throws an IOException exception, and any attempt to read fails and throws a NotSupportedException exception.

Tim Schmelter
- 450,073
- 74
- 686
- 939
-
Good, but what about reading from the source file? I will need to stream append the contents of one file to the end of the other file without reading all of it to memory at once, therefore the stream part of it... – Daniel Jun 18 '13 at 14:45
-
@DanielVallandTorgrimsen: Have a look, edited my answer to provide a streaming approach. However, not fully tested. – Tim Schmelter Jun 18 '13 at 15:20
-
It seams to be copying each byte in a loop.. are you sure this is the most effective way? thanks :) – Daniel Jun 18 '13 at 16:50
-
@Daniel: specify efficient first. In Terms of Memory consumption? Definitively. – Tim Schmelter Jun 18 '13 at 16:56
-
Measure it. Consider that methods that write multiple bytes at once also need loops, perhaps at a deeper level. – Tim Schmelter Jun 18 '13 at 17:20
-
I have compared them, and a buffer that writes multiple bytes at a times seams a few seconds faster... :) but the code works great :) – Daniel Jun 18 '13 at 17:38
-
@DanielVallandTorgrimsen - please consider accepting this as the answer – Matt Wilko Jun 20 '13 at 13:35