logo VB.Net - Read or Write Binary File
Guide Contents
DigitalDan Home Page

Read Bytes from Binary File
There are two approaches, the first opens the file with "Dim" and the second involves "Using" You can use either of these approaches in most applications. These examples involve the "Using" approach.
Readers who prefer the "Dim" approach can convert the examples as follows...
Replace the first "Using" with "Dim"
Replace the "End Using" with "fs1.Close: fs1.Dispose"
 
When setting startByte, count the bytes starting at zero (first byte in file)
NumberOfBytes should be set to the number of bytes to be read from the file

 
Private Shared Function ReadBinary(filename As String, startByte As Integer, NumberOfBytes As Integer) As Byte()
Dim buffer() As Byte = New Byte(NumberOfBytes - 1) {}
Dim errorMessage As String = String.Empty
Try
Using fs1 As New FileStream(filename, IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.None)
fs1.Position = startByte
fs1.Read(buffer, 0, buffer.Length)
End Using
Catch ex As Exception
errorMessage = ex.Message
End Try
Return buffer
End Function

 
if you changed "FileAccess.Read" to "FileAccess.ReadWrite" in the line starting "Using fs1 ..." then
you could read bytes from and write bytes to the file within the same "Using" block of code!
 
Read All Bytes from Binary File
This example reads the entire file into an array of Byte.
 
Private Shared Function ReadBinaryFile(fileName As String) As Byte()
Dim ret() As Byte
ret = Array.Empty(Of Byte)
Dim errorMessage As String = String.Empty
Try
ret = My.Computer.FileSystem.ReadAllBytes(fileName)
Catch ex As Exception
errorMessage = ex.Message
End Try
Return ret
End Function

Write Bytes to Binary File
Use extreme caution when writing to binary files - data formats are not consistent. It is easy to corrupt files, break programs and mess-up computers by making inappropriate changes
 
There are two approaches, the first opens the file with "Dim" and the second involves "Using" You can use either of these approaches in most applications. These examples involve the "Using" approach.
Readers who prefer the "Dim" approach can convert the examples as follows...
Replace the first "Using" with "Dim"
Replace the "End Using" with "fs1.Close: fs1.Dispose"
 
When setting startByte, count the bytes starting at zero (first byte in file)
In this example, the number of bytes to be replaced will equal the number of bytes in the array. All other file contents will remain unchanged
 
Private Shared Function WriteBinary(filename As String, startByte As Integer, newBytes As Byte()) As String
Dim errorMessage As String = String.Empty
Try
Using fs As New FileStream(filename, IO.FileMode.Open, IO.FileAccess.Write, IO.FileShare.None)
fs.Position = startByte
fs.Write(newBytes, 0, newBytes.Length)
End Using
Catch ex As Exception
errorMessage = ex.Message
End Try
Return errorMessage
End Function

 
if you changed "FileAccess.Write" to "FileAccess.ReadWrite" in the line starting "Using fs1 ..." then you could read bytes from and write bytes to the file within the same "Using" block of code!
 
Write Binary File to match entire contents of array of byte
This example writes the entire file setting all the file contents to match the bytes in bArray
 
Private Shared Function WriteBinaryFile(FileName As String, bArray() As Byte) As String
Dim errorMessage As String = String.Empty
Try
My.Computer.FileSystem.WriteAllBytes(FileName, bArray, False)
Catch ex As Exception
errorMessage = ex.Message
End Try
Return errorMessage
End Function

DigitalDan.co.uk ... Hits = 236