I'm currently reading a file in through the binaryreader class and I can successfully write it back out using the binarywriter class.
What I am trying to find out is how to insert data into the file. Whether reading it all in, inserting what I need to and recreating it byte for byte I don't mind.
binarywriter doesn't do what I need to do as it doesn't insert, only writes over.
What options do I have on inserting data (shorts/bytes ect) into a file.
Loading

VulpesRank #55 · 29.2K reputationPosted May 24, 2013, 3:08 PM EDT
Jay S159 reputationPosted May 26, 2013, 6:53 AM EDT
If I read it in byte by byte into a list, what if I want to add a short?.
Also, by reading it into a list as suggested, and then going to index 5 to insert, what if I only know the hex/decimal offset where I want to insert rather than the lists index?
VulpesRank #55 · 29.2K reputationPosted May 26, 2013, 7:26 AM EDT
As it's a List of bytes, the index into the List will be the same as the offset into the file - whether this is expressed as decimal or hexadecimal.
So, to insert 65 at offset 0x1F, you'd do:
bytes.Insert(0x1F, (byte)65);
Jay S159 reputationPosted May 26, 2013, 7:33 AM EDT
Is putting each byte into a list the only way or are there alternatives?
VulpesRank #55 · 29.2K reputationPosted May 26, 2013, 7:42 AM EDT
You could then use the appropriate BinaryWriter method to make the insertion (Write(Int16) for a short) and then carry on reading the existing bytes and writing them to the second file until you get to the end.
You'd then need to delete the first file and rename the second file to the first.
This is not as neat but has the advantage that you don't need to read the entire file into memory first.