Fire Hand
Fire Hand

Reputation: 26346

Read from binary file and convert byte array into string

I'm trying to read the binary data from a binary file with the code below but the it's return the value in the byte array. How can i read the binary data from the binary file and then convert the data into string?

This is how i create the binary file.

Dim fs As New FileStream(Application.StartupPath & "\Agency.dat", FileMode.OpenOrCreate)
    Dim bf As New BinaryFormatter()
    Call bf.Serialize(fs, GAgency)
    fs.Close()

Code to read the binary file.

Public Shared Function ReadBinaryFile(ByVal path As String)
    ' Open the binary file.
    Dim streamBinary As New FileStream(path, FileMode.Open)

    ' Create a binary stream reader object.
    Dim readerInput As BinaryReader = New BinaryReader(streamBinary)

    ' Determine the number of bytes to read.
    Dim lengthFile As Integer = FileSize(path)

    ' Read the data in a byte array buffer.
    Dim inputData As Byte() = readerInput.ReadBytes(lengthFile)

    ' Close the file.
    streamBinary.Close()
    readerInput.Close()

    Return inputData
End Function 'ReadBinaryData’

Upvotes: 0

Views: 7615

Answers (1)

Damith
Damith

Reputation: 63065

try as

Dim objGAgency As GAgency
Dim fs As Stream = New FileStream( Application.StartupPath & "\Agency.dat", FileMode.Open)
Dim bf As BinaryFormatter = New BinaryFormatter()

objGAgency = CType(bf.Deserialize(fs), GAgency)
fs.Close()

Upvotes: 1

Related Questions