Imports System.IO
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'specify the filename
Dim strFilename As String = My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\Test.tmp"
'create a filestream object, this is something like a file handle in VB6
Dim fsOutput As New FileStream(strfilename, FileMode.Create, FileAccess.Write, FileShare.None)
'create a binary writer object, that knows how to encode variables into a file
Dim bwOutput As New BinaryWriter(fsOutput)
'set up some variables to write
Dim intOne As Integer = 12
Dim strTwo As String = "abcdefg"
'write the integer
bwOutput.Write(intOne)
'write the string
bwOutput.Write(strTwo)
'close and dispose of file writing objects
bwOutput.Close()
fsOutput.Close()
fsOutput.Dispose()
'create a filestream object, this is something like a file handle in VB6
Dim fsInput As New FileStream(strFilename, FileMode.Open, FileAccess.Read, FileShare.None)
'create a binary reader object, that knows how to decode variables from a file
Dim brInput As New BinaryReader(fsInput)
'set up target variables
Dim intThree As Integer
Dim strFour As String
'read the integer
intThree = brInput.ReadInt32()
'read the string
strFour = brInput.ReadString
'display the variables so we know they loaded correctly
MsgBox("Integer = " & intThree.ToString & vbCr & "String = " & strFour)
'close and dispose the file reading objects
brInput.Close()
fsInput.Close()
fsInput.Dispose()
'delete the file
Kill(strFilename)
End Sub
End Class