Adding data to a byte array

I am currently reading data from a binary file (File.ReadAllBytes), converting this byte array to a string and adding data to this string. Finally, I convert the string back to an array of bytes and write the data back to a new file.

Yes, this method is pretty idiotic, and I was curious if there is a way to add this new data to the end of the byte array (as 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 are functions that convert a string> Byte> string.

What I would like to do:

 b[] = file.readallbytes(s) b = b + "new Data" file.writeallbytes(b[]) 

Thanks so much for any information on this.

+8
string c # bytearray
source share
5 answers

You have to get used to working with Streams - in this case, you could use a MemoryStream to achieve the same thing without all these 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()); } 

It is possible that this looks more complicated than your code, but by all accounts, it is more efficient.

Of course, if you just write another file (or even the same file), you can simply write directly to the file and skip the need for a byte or MemoryStream array completely - this is the beauty of streams.

+16
source share

Use FileStream , find 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 entire file, just use Array.Resize to make your buffer larger, and copy the part you want to add.

 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 is no “shorthand” for this, sorry.: \


If the whole point of this is to add a line, just use File.AppendAllText !

+5
source share

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

+3
source share

If you just want to add something to the file, then you should use FileStream and StreamWriter:

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

or something like that.

If you just want to add a few 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); } 
+2
source share

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

0
source share

All Articles