Currently, I am reading data from a binary file (File.ReadAllBytes), converting this byte array into a string and appending data onto this string. Lastly, I am converting the string back into a byte array and writing the data back into a new file.
Yeah – this method is fairly idiotic, and I’ve been curious as to whether or not there is some way to append this new data onto the end of the byte array (in the form of a byte).
String s = @"C:\File.exe";
Byte b[] = File.ReadAllBytes(s);
String NewString = ConvertToString(b[]);
NewString = NewString + "Some Data";
b[] = ConvertToByteArray(NewString);
File.WriteAllBytes(b[]);
// ConvertToByteArray and ConvertToString represent functions that converts string > Byte > string.
What I would like to do:
b[] = file.readallbytes(s)
b = b + "new Data"
file.writeallbytes(b[])
Thank you very much for any insight on the matter.
You should get used to working with Streams – in this case you could use a
MemoryStreamto achieve the exact same thing without all those nasty arrays.Admitedly this looks a whole load more complicated than your code, but under the covers what it’s doing is more efficient.
Of course if you are just writing to another file (or even the same file) you can simply write directly to the file and skip the need for a
bytearray orMemoryStreamentirely – this is the beauty of streams.