VB - How to read and write a binary file?

How can I read the original byte array from any file ...

Dim bytes() as Byte 

.. and then write this byte array to a new file?

I need it as an array of bytes to do some processing between them.


I am currently using:

To read

  Dim fInfo As New FileInfo(dataPath) Dim numBytes As Long = fInfo.Length Dim fsAs New FileStream(dataPath, FileMode.Open, FileAccess.Read) Dim br As New BinaryReader(fs) Dim bytes As Byte() = br.ReadBytes(CInt(numBytes)) br.Close() fs.Close() 

To write

 Dim fs As System.IO.FileStream fs = New System.IO.FileStream(outpath, System.IO.FileMode.Create) fs.Write(bytes, 0, bytes.Length) fs.Close() 
+6
filestream bytearray binaryfiles
source share
3 answers
 Dim data() as Byte = File.ReadAllBytes(path1) File.WriteAllBytes(path2, data) 
+15
source share
 System.IO.File.ReadAllBytes("myfile.txt") 
+5
source share

Try the following: -

 Dim bytes() as Byte bytes = File.ReadAllBytes(fileName) '' # Do stuff to the array File.WriteAllBytes(otherFileName, bytes) 
+3
source share

All Articles