c# - Append Data to Byte Array -


currently, reading data binary file (file.readallbytes), converting byte array string , appending data onto string. lastly, converting string byte array , writing data new file.

yeah - method idiotic, , i've been curious whether or not there way append new data onto end of byte array (in form of 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 , converttostring represent functions converts string > byte > string.

what do:

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

thank insight on matter.

you should used working streams - in case use memorystream achieve exact same thing without nasty arrays.

byte[] bytes = file.readallbytes(infilepath); using (memorystream ms = new memorystream()) {     // use streamwriter "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 looks whole load more complicated code, under covers it's doing more efficient.

of course if writing file (or same file) can write directly file , skip need byte array or memorystream entirely - beauty of streams.


Comments

Popular posts from this blog

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -