I need to write some code that write X number of bytes:
byte[] byteArray = MyFunc.getData();
File.WriteAllBytes(fileName, byteArray);
this perfectly work. Next I need to write a custom object at the end or at the start of the file, I do the following:
byte[] myObjectArray;
BinaryFormatter bf = new BinaryFormatter();
using (MemoryStream ms = new MemoryStream())
{
bf.Serialize(ms, myObject);
myObjectArray = ms.ToArray();
}
Now my question is: having this new array of byte, how can I write and read it from a file? The first byte array that I write has variable number of bytes, and also the second one can have different dimension. Can you please help me? Thanks.
I'm not sure what you are asking about but if you question is only about writing new byte array to the end of file, simply open stream to file and use
stream.write(byte[], int, int)
method. If you need to find that array in future i suggest to make some kind of marker that tell you "here is your object that you searching for", for example:[ xxxxx byte[] xxxxx ] [ #### MARKER #### ] [ xx byte[] xx]
As you can see, every byte after marker is that array you searching for.