I am working on a steganography software in C#, more precisely for video files. My approach is to append the extra information at the end of a video file. However, I must read the whole video file in memory first. I used the File.ReadAllBytes() function in C# to read a video file (video around 200MB) into a byte array. Then I create a new array with the video's bytes and my data's bytes. But, this sometimes causes an OutOfMemoryException. And when it doesn't it is very slow. Is there a more efficient way to append bytes to an existing file in C# which will solve this issue? Thank you.
-
Loading a 200MB file into memory is the wrong approach (ignoring one-off scenarios). You should use a stream instead. – xxbbcc Jan 12 '15 at 14:43
-
http://stackoverflow.com/questions/5958495/append-data-to-byte-array?rq=1 check this .. – mybirthname Jan 12 '15 at 14:44
-
possible duplicate of [C# Append byte array to existing file](http://stackoverflow.com/questions/6862368/c-sharp-append-byte-array-to-existing-file) – Reti43 Jan 12 '15 at 15:43
3 Answers
Open the file with FileMode.Append
var stream = new FileStream(path, FileMode.Append)
FileMode.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.

- 18,620
- 8
- 71
- 89
Sure, it's easy:
using (var stream = File.Open(path, FileMode.Append))
{
stream.Write(extraData);
}
No need to read the file first.
I wouldn't class this as steganography though - that would involve making subtle changes to the video frames such that it's still a valid video and looks the same to the human eye, but the extra data is encoded within those frames so it can be extracted later.

- 1,421,763
- 867
- 9,128
- 9,194
attempt this method, I am unsure if it will yield faster results, but logically it should. : https://stackoverflow.com/a/6862460/2835725

- 1
- 1

- 154
- 6
-
1
-
-
1If you are unable to comment, it doesn't mean you should go ahead an post an answer suited for a [comment](http://stackoverflow.com/help/privileges/comment). Also, avoid link only answers. Acknowledge the source, but copy the relevant answer/code here. This is also a case of a duplicate question, where a flag would have been more appropriate. – Reti43 Jan 12 '15 at 15:45