Filestream help required

mickle026

Member
Joined
Oct 31, 2009
Messages
20
Programming Experience
3-5
Is there a correct way of doing this in vb.net?

I want to read a file as a set of numbers such as long integers ,say the file contains the string "Hello World - Its a beautiful day blah blah blah"

This would read as a set of long integers that are 4 bytes long

ie ascii (hex)
48 65 6C 6C 6F 20 57 6F
H e l l o W o

etc and that would also be an 4 byte word represented as a number 1819043144 thus stepping through the file 4 bytes at a time.
Instead of reading and looking for certain words in the file I would look for certain numbers, thus eliminating read errors in certain file types.

I am new to vb.net, and have coded this type of thing in vb6 before , this would be done with the 'get' method and a file pointer.

I have managed to read it a byte at a time with this code but I am unsure where to go with it as the 'get' method isn't in vb.net
VB.NET:
Imports System.IO
Imports System.Text

Public Class Form1

    Dim Infile As String
    Dim StopApplication As Boolean

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        OpenFileDialog1.ShowDialog()
        Infile = OpenFileDialog1.FileName
        ReadBytes()
    End Sub

    Public Sub ReadBytes()
        Dim myFileStream As FileStream
        Dim intByte As Integer
        Dim lngLoop As Long = 0
        Dim bteRead() As Byte
        Dim fFile As New FileInfo(Infile)
        Dim fSize As Integer = fFile.Length
        lblFilesize.Text = "Filesize " & CStr(fSize) & " bytes"
        Try
            myFileStream = File.OpenRead(Infile)
            ReDim bteRead(myFileStream.Length)

            For a = 0 To fSize - 1
                intByte = myFileStream.ReadByte()
                If intByte <> -1 Then bteRead(lngLoop) = CByte(intByte)
                lngLoop += 1
                lblStatus.Text = "Reading: " & lngLoop
                If StopApplication = True Then Exit Sub
                Application.DoEvents()
            Next

            Console.WriteLine(Encoding.ASCII.GetString(bteRead))
            myFileStream.Close()

        Catch ex As IOException
            Console.WriteLine(ex.Message)
        End Try
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonStopApplication.Click
        StopApplication = True
    End Sub
End Class

Any help would be appreciated
 
One point to note that, unlike VB6, in VB.NET a 4-byte number is an Integer, not a Long. A VB.NET Long is 8 bytes.

As for the question, you could create a BinaryReader and call ReadInt32. You might also just call File.ReadAllBytes to create a Byte array containing the whole file, then use the BitConverter class to read 4 bytes at a time as an Integer.
VB.NET:
Dim number As Integer

Try
    Using reader As New BinaryReader(File.Open(filePath, FileMode.Open))
        Do Until reader.PeekChar() = -1
            number = reader.ReadInt32()

            'Use number here.
        Loop
    End Using
Catch ex As Exception
    'There were not 4 bytes available, or something else went wrong.
End Try




Dim data As Byte() = File.ReadAllBytes(filePath)

For index As Integer = 0 To data.GetUpperBound(0) Step 4
    number = BitConverter.ToInt32(data, index)

    'Use number here.
Next
 
Last edited:
Back
Top