Imports System.io Public Class Form1 Inherits System.Windows.Forms.Form #Region " Windows Form Designer generated code " Public Sub New() MyBase.New() 'This call is required by the Windows Form Designer. InitializeComponent() 'Add any initialization after the InitializeComponent() call End Sub 'Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub Friend WithEvents Button1 As System.Windows.Forms.Button Friend WithEvents TextBox1 As System.Windows.Forms.TextBox Friend WithEvents Button2 As System.Windows.Forms.Button 'Required by the Windows Form Designer Private components As System.ComponentModel.Container 'NOTE: The following procedure is required by the Windows Form Designer 'It can be modified using the Windows Form Designer. 'Do not modify it using the code editor. Private Sub InitializeComponent() Me.Button1 = New System.Windows.Forms.Button() Me.TextBox1 = New System.Windows.Forms.TextBox() Me.Button2 = New System.Windows.Forms.Button() Me.SuspendLayout() ' 'Button1 ' Me.Button1.Font = New System.Drawing.Font("Verdana", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.Button1.Location = New System.Drawing.Point(8, 8) Me.Button1.Name = "Button1" Me.Button1.Size = New System.Drawing.Size(264, 32) Me.Button1.TabIndex = 0 Me.Button1.Text = "Save Records" ' 'TextBox1 ' Me.TextBox1.Font = New System.Drawing.Font("Verdana", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.TextBox1.Location = New System.Drawing.Point(8, 88) Me.TextBox1.Multiline = True Me.TextBox1.Name = "TextBox1" Me.TextBox1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical Me.TextBox1.Size = New System.Drawing.Size(264, 176) Me.TextBox1.TabIndex = 1 Me.TextBox1.Text = "" ' 'Button2 ' Me.Button2.Enabled = False Me.Button2.Font = New System.Drawing.Font("Verdana", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.Button2.Location = New System.Drawing.Point(8, 48) Me.Button2.Name = "Button2" Me.Button2.Size = New System.Drawing.Size(264, 32) Me.Button2.TabIndex = 2 Me.Button2.Text = "Read Records" ' 'Form1 ' Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(280, 269) Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Button2, Me.TextBox1, Me.Button1}) Me.Name = "Form1" Me.Text = "StreamWriter/Reader Demo" Me.ResumeLayout(False) End Sub #End Region Structure Product Dim ProdID As String Dim prodDescription As String Dim listPrice As Single Dim Available As Boolean Dim minStock As Integer End Structure Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim BW As BinaryWriter Dim FS As FileStream FS = New FileStream("BinaryData.bin", _ System.IO.FileMode.OpenOrCreate, _ System.IO.FileAccess.Write) BW = New BinaryWriter(FS) FS.Seek(0, SeekOrigin.Begin) Dim p As New Product() p.ProdID = "100-A39" p.prodDescription = "Cellular Phone with built-in TV" p.listPrice = 497.99 p.Available = True p.minStock = 40 SaveRecord(BW, p) p = New Product() p.ProdID = "100-U300" p.prodDescription = "Wireless Handheld" p.listPrice = 315.5 p.Available = False p.minStock = 12 SaveRecord(BW, p) p = New Product() p.ProdID = "99-BBB" p.prodDescription = "Calculator/Recorder Watch" p.listPrice = 49.99 p.Available = True p.minStock = 10 SaveRecord(BW, p) BW.Close() FS.Close() FS = Nothing MsgBox("Records saved") Button2.Enabled = True End Sub Sub SaveRecord(ByVal writer As BinaryWriter, ByVal record As Product) writer.Write(record.ProdID) writer.Write(record.prodDescription) writer.Write(record.listPrice) writer.Write(record.Available) writer.Write(record.minStock) End Sub Sub ShowRecord(ByVal record As Product) TextBox1.AppendText(vbCrLf) TextBox1.AppendText(record.prodDescription & vbCrLf) TextBox1.AppendText(record.listPrice.ToString & vbCrLf) TextBox1.AppendText(record.Available & vbCrLf) TextBox1.AppendText(record.minStock.ToString & vbCrLf) End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Dim BR As BinaryReader Dim FS As FileStream FS = New System.IO.FileStream("Binarydata.bin", _ FileMode.Open, _ FileAccess.Read) BR = New System.IO.BinaryReader(FS) BR.BaseStream.Seek(0, SeekOrigin.Begin) Dim p As New Product() TextBox1.Clear() Dim c As Integer c = BR.PeekChar While FS.Position < FS.Length Console.WriteLine(c) p = Nothing ' READ FIELDS AND POPULATE STRUCTURE p.ProdID = BR.ReadString p.prodDescription = BR.ReadString p.listPrice = BR.ReadSingle p.Available = BR.ReadBoolean p.minStock = BR.ReadInt32 ' DISPLAY STRUCTURE ShowRecord(p) c = BR.PeekChar End While BR.Close() FS.Close() End Sub End Class