Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleUse a BinaryReader and BinaryWriter in loops to read and write an array of integers in a file in Visual Basic 2005
DescriptionThis example shows how to use a BinaryReader and BinaryWriter in loops to read and write an array of integers in a file in Visual Basic 2005.
KeywordsBinaryReader, BinaryWriter, Visual Basic 2005, VB 2005, VB.NET
CategoriesFiles and Directories, VB.NET
 
When the program starts, the form's Load event handler saves references to its TextBoxes in an array for later use. It then determines whether its data file exists. If the file exists, the program makes a FileStream attached to it and a BinaryReader associated with the FileStream. It then loops through the file using the BinaryReader's ReadInt32 method to read the integers stored in the file. Finally it displays the results in the TextBoxes.
 
Private Const NUM_VALUES As Integer = 10

Private m_TextBoxes() As TextBox

' Load saved values.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal _
    e As System.EventArgs) Handles MyBase.Load
    m_TextBoxes = New TextBox() {TextBox1, TextBox2, _
        TextBox3, TextBox4, TextBox5, TextBox6, TextBox7, _
        TextBox8, TextBox9, TextBox10}

    ' Get the file name.
    Dim file_name As String = GetFileName()

    ' See if the file exists.
    If Exists(file_name) Then
        ' Open the file.
        Dim fs As New FileStream(file_name, FileMode.Open)

        ' Create a BinaryReader for the FileStream.
        Dim binary_reader As New BinaryReader(fs)
        fs.Position = 0

        ' Read the integer values.
        Dim integers(NUM_VALUES - 1) As Integer
        For i As Integer = 0 To NUM_VALUES - 1
            integers(i) = binary_reader.ReadInt32()
        Next i
        binary_reader.Close()
        fs.Dispose()

        ' Display values.
        For i As Integer = 0 To NUM_VALUES - 1
            m_TextBoxes(i).Text = integers(i).ToString()
        Next i
    End If
End Sub
 
When the program's form is closing, the following code saves the current integer values into the data file. It makes aFileStream associated with the file and creates a BinaryWriter for the FileStream. It loops through the TextBoxes copying their values into an array of Integer. Finally it loops through the array using the BinaryWriter's Write method to write the Integers into the file.
 
' Save the current values.
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal _
    e As System.Windows.Forms.FormClosingEventArgs) Handles _
    Me.FormClosing
    ' Get the file name.
    Dim file_name As String = GetFileName()

    ' Create the file.
    Using fs As New FileStream(file_name, FileMode.Create)
        ' Create a BinaryWriter for the FileStream.
        Dim binary_writer As New BinaryWriter(fs)

        ' Get the values.
        Dim integers(NUM_VALUES - 1) As Integer
        For i As Integer = 0 To NUM_VALUES - 1
            integers(i) = Val(m_TextBoxes(i).Text)
        Next i

        ' Write the values.
        For i As Integer = 0 To NUM_VALUES - 1
            binary_writer.Write(integers(i))
        Next i
        binary_writer.Close()
    End Using
End Sub
 
 
Copyright © 1997-2006 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated