10

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.

5 Answers 5

18

You should get used to working with Streams - in this case you could use a MemoryStream to achieve the exact same thing without all those nasty arrays.

byte[] bytes = File.ReadAllBytes(inFilePath);
using (MemoryStream ms = new MemoryStream())
{
    // You could also just use StreamWriter to do "writer.Write(bytes)"
    ms.Write(bytes, 0, bytes.Length);

    using (StreamWriter writer = new StreamWriter(ms))
    {
        writer.Write("Some Data");
    }

    File.WriteAllBytes(outFilePath, ms.ToArray());
}

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 byte array or MemoryStream entirely - this is the beauty of streams.

Sign up to request clarification or add additional context in comments.

1 Comment

This worked exactly as I had envisioned. Thank you very much for your time. (All of you).
5

Use a List<Byte>, add every byte from the initial ReadAllBytes using AddRange, and then add the next set of bytes. Finally, use CopyTo to copy all bytes to an array (of size List.Length).

1 Comment

I was just curious, do you think you could give me a quick example of how to join the following? byte[] bData; string sData = "test data"; bData = bData + Convert.ToByte(sData); I did some reading on list<byte> but I'm still somewhat confused.
5

Use FileStream, seek to the end of the file, then write what you need:

using (var fs = new FileStream(s, FileMode.Open, FileAccess.ReadWrite))
{
    fs.Seek(SeekOrigin.End);
    fs.Write(data, 0, data.Length);
}

If you really need to read the whole file, then just use Array.Resize to make your buffer bigger, and copy over the part you want to append.

var data = File.ReadAllBytes(s);
Array.Resize(ref data, data.Length + toAppend.Length);
Array.Copy(toAppend, 0, data, data.Length - toAppend.Length, toAppend.Length);
File.WriteAllBytes(s, data);

There's no "shorthand" for this, sorry. :\


If the entire point of this is to append a string, just use File.AppendAllText!

2 Comments

are you suggesting that I use filestream to read the binary file as well?
@Evan: No, sorry, see my edit. That was assuming you didn't actually need to read the entire file; if you do, use Array.Resize.
2

If you just want to append something to the file, then you should be using a FileStream and a StreamWriter:

        using (var f = File.Open(@"C:\File.exe", FileMode.Append))
        using (var w = new StreamWriter(f))
            w.Write("new Data");

or something similar to this.

If you just want to append some bytes:

        using (var f = File.Open(@"C:\File.exe", FileMode.Append))
        {
            byte[] buf = new byte[] { 69, 65, 66, 67 };
            f.Write(buf, 0, buf.Length);
        }

Comments

0

Create a new array with size of the sum of two separate arrays, then copy your arrays to it.

2 Comments

I think by "sum" you mean "concatenation" or something, right?
@Mehrdad a1.Lenght + a2.Lenght that is. My English is not good, sad, but true :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.