logo  VB.Net - Binary Files
This Chapter
File Attributes
Binary File
Column Header
Copy Delete ...
CSV File
File Path
Text File
Chapters
Home Page
Colours, RGB
Computer Specifications
Dates&Times
Disk Drives
Files
Folders
GPS and OS Ref
VB.Net Forms
Image Files
If & Select
List/Array
Mathematics
NuGet
Sound
String Functions
Sun and Moon
User Controls
Validation
DigitalDan Sites
My Other Sites
Contact Site

Note
Some pages
may contain
inaccuracies
Hits=5
Binary Files
Binary files are a sequence of bytes stored for future use. They could contain images, text, programs, data or anything else. The binary file routines can read/write any byte (or sequence of bytes) in the file without reading the previous bytes. When accessing any file (including binary) you should enclose the function in a Try...Catch block and try to handle unexpected errors e.g. disk full or file locked.
 
You can use these Binary File functions with any type of file, including Text and CSV files. Although some text files use one byte per letter, many formats require 2 bytes per letter. The binary reader pulls data 1 byte at a time and your program may need code to interpret 2 byte characters
 
Avoid editing any biinary file unless you fully understand the file format. It is very easy to render the file permenantly unreadable.
 '
Reading From A Binary File
Read Start of Binary File

Private Function ReadBinary_FirstBytes(filename As String, NumberOfBytes As Integer) As List(Of Byte)
 Dim bytesRead As Integer
 Dim buffer(NumberOfBytes) As Byte 'Default buffer size is 4096
 Using inFile As New IO.FileStream(filename, FileMode.Open)
  bytesRead = inFile.Read(buffer, 0, buffer.Length)
 End Using
 Return buffer.ToList
End Function
  
Read Binary File - Block at a Time

Private Sub ReadBinary_BlockAtATime(filename As String, blockSize As Integer)
 Dim buffer(blockSize - 1) As Byte
 Dim bytesRead As Integer = 9999 ' dummy value > 0
 Using stream As New IO.FileStream(filename, FileMode.Open)
  While bytesRead > 0
   bytesRead = stream.Read(Buffer, 0, Buffer.Length)
   If bytesRead > 0 Then
    '
    ' add logic to process the current block of date here
    '
   End If
  End While
 End Using
End Sub
  
Read Binary File - Bytes from Middle of File

Private Function ReadBinary_MiddleOfFile(filename As String, startPosition As Integer, numberOfBytes As Integer) As List(Of Byte)
 Using stream = File.OpenRead(filename)
  stream.Position = startPosition
  Dim bytes(numberOfBytes - 1) As Byte
  stream.ReadExactly(bytes, startPosition, numberOfBytes)
  Return bytes.ToList
 End Using
End Function
  
Read Binary File - Entire File, All at Once
Consider checking file size before asking computer to read an entire file. It could attempt to drop entire contents of huge (> 4GB) file into memory!

Private Function ReadBinary_AllBytes(filename As String) As List(Of Byte)
 Return IO.File.ReadAllBytes(filename).ToList
End Function
  
Writing To A Binary File
Overwrite Part of Binary File
File corruption is a common occurence when trying to overwrite part of a binary file. You should ensure that you fully understand te hfile format and consequences of your changes. Suppose we have a simple binary file whose contents are
   Daniel1Freda 2Joe   3Mike  4
  

The file format is 6 bytes for the name, 1 byte for the number. We realise that Joe wants to be called Josephine and overwrite the name. Our file now contains
   Daniel1Freda 2Josephineke  4
  

the file is corrupted - the computer will see an "i" instead of Joe's number and "neke" instead of "Mike"

Private Sub OverwriteBinary_MiddleOfFile(filename As String, startPosition As Integer, ListBytes As List(Of Byte))
 Using stream = File.OpenWrite(filename)
  stream.Position = startPosition
  stream.Write(ListBytes.ToArray)
 End Using
End Sub
  
Add Bytes to End of Binary File

Private Sub Append(filename As String, ListBytes As List(Of Byte))
 Using fs As New IO.FileStream(filename, IO.FileMode.Append)
  Using bw As New IO.BinaryWriter(fs)
   bw.Write(ListBytes.ToArray)
  End Using
 End Using
End Sub
  
Rewrite Binary File Using All Bytes from List

Private Sub WriteBinary_AllBytes(filename As String, ListBytes As List(Of Byte))
    IO.File.WriteAllBytes(filename, ListBytes.ToArray)
End Sub
  

DigitalDan.co.uk