You can still use VB6 code such as fileget and fileput in VB.NET (Option Explicit Off)
or if you are reading fixed length files then some code to help you on your way.
Public Class RandomFile
Private fs As FileStream, sr As BinaryReader, sw As BinaryWriter
Private FieldString(), fpath As String
Private FieldCount, FieldPos(), FieldWidths(), FileLength, LineCount, LineWidth As Integer
Private Open As Boolean = True
Public Sub New(ByVal Path As String)
fpath = Path
If File.Exists(Path) Then
Dim f As New FileInfo(fpath)
FileLength = CInt(f.Length)
Else
FileLength = 0
End If
End Sub
Public Sub Dispose()
fs.Dispose()
End Sub
#Region " Properties "
Public ReadOnly Property Count() As Integer ' Number of records in file
Get
Return LineCount
End Get
End Property
Public Property Width() As Integer
Get
Return LineWidth
End Get
Set(ByVal value As Integer)
LineWidth = value
LineCount = FileLength \ LineWidth - 1 ' and total number of lines in file
End Set
End Property
Public ReadOnly Property Strings() As String()
Get
Return FieldString
End Get
End Property
Public WriteOnly Property Widths() As Integer()
Set(ByVal value As Integer())
FieldWidths = value ' set externally
FieldCount = UBound(FieldWidths) ' number of fields
ReDim FieldPos(FieldCount), FieldString(FieldCount) ' set the number of strings to be returned
LineWidth = 0 ' total length of a record
For i As Integer = 0 To FieldCount
FieldPos(i) = LineWidth ' start position of any field
LineWidth += FieldWidths(i) ' calculate file line width
Next
LineCount = FileLength \ LineWidth - 1 ' and total number of lines in file
End Set
End Property
#End Region
#Region " Read File "
Private Function ReadFieldBytes(ByVal Record As Integer, ByVal Index As Integer) As Byte()
fs = New FileStream(fpath, FileMode.Open, FileAccess.Read, FileShare.Read)
sr = New BinaryReader(fs)
fs.Seek(LineWidth * Record + FieldPos(Index), SeekOrigin.Begin)
Dim b() As Byte = sr.ReadBytes(FieldWidths(Index))
Return b
End Function
Private Function ReadFieldString(ByVal Record As Integer, ByVal Index As Integer) As String
Dim b() As Byte = ReadFieldBytes(Record, Index)
Dim s As String = ""
For i = 0 To UBound(b)
s &= Chr(b(i))
Next i
Return s
End Function
Public Function ReadBytes(ByVal Record As Integer) As Byte()
Try
fs = New FileStream(fpath, FileMode.Open, FileAccess.Read, FileShare.Read)
sr = New BinaryReader(fs)
fs.Seek(LineWidth * Record, SeekOrigin.Begin)
Dim b() As Byte = sr.ReadBytes(LineWidth) ' read length of each field
Return b ' close reader externally in case we want more
Catch ex As Exception
Return Nothing
End Try
End Function
Public Function ReadLine(Optional ByVal Record As Integer = 0) As String() ' read one line
Try
If Open Then
fs = New FileStream(fpath, FileMode.Open, FileAccess.Read, FileShare.Read)
sr = New BinaryReader(fs)
End If
fs.Seek(LineWidth * Record, SeekOrigin.Begin)
For Field As Integer = 0 To FieldCount ' read each field in turn
Dim b() As Byte = sr.ReadBytes(FieldWidths(Field)) ' read length of each field
FieldString(Field) = ""
For i = 0 To UBound(b)
FieldString(Field) &= Chr(b(i)) ' and convert it into string
Next i
Next Field
Open = False ' you may want to read more lines
Return FieldString ' close reader externally in case we want more
Catch ex As Exception
Return Nothing
End Try
End Function
Public Sub CloseReader()
sr.Close()
fs.Close()
fs.Dispose()
Open = True
End Sub
#End Region
#Region " Write File "
Public Function WriteField(ByVal Record As Integer, ByVal Index As Integer, ByVal Field() As String) As Boolean
Dim f As Integer = UBound(Field) ' length of array
Dim k As Integer = FieldWidths(Index) ' width of each line
Dim b(k * (f + 1) - 1) As Byte ' total number of bytes
For i As Integer = 0 To f
Dim s As String = Field(i).PadRight(k, cnBlank) ' Ensure string is width of field
For j = 0 To k - 1
b(i * k + j) = Asc(s.Substring(j)) ' convert string to bytes
Next
Next
Return WriteRecord(LineWidth * Record + FieldPos(Index), b) ' calculate start
End Function
Public Function WriteField(ByVal Record As Integer, ByVal Index As Integer, ByVal Field As String) As Boolean
Dim k As Integer = FieldWidths(Index) - 1
Field = Field.PadRight(k + 1, cnBlank) ' Ensure string is width of field
Dim b(k) As Byte ' and set bytes to match
For i As Integer = 0 To k
b(i) = Asc(Field.Substring(i)) ' convert string to bytes
Next
Return WriteRecord(LineWidth * Record + FieldPos(Index), b) ' calculate start
End Function
Public Function WriteField(ByVal Record As Integer, ByVal Offset As String, ByVal b() As Byte) As Boolean
Return WriteRecord(LineWidth * Record + Val(Offset), b) ' calculate start
End Function
Public Function WriteField(ByVal Record As Integer, ByVal Index As Integer, ByVal b() As Byte) As Boolean
Return WriteRecord(LineWidth * Record + FieldPos(Index), b) ' calculate start
End Function
Public Function WriteLine(ByVal Record As Integer, ByVal b() As Byte, Optional ByVal chk As Boolean = True) As Boolean
If chk Then
Return WriteRecord(LineWidth * Record, b) ' closes writer
Else
Try
If Open Then
fs = New FileStream(fpath, FileMode.Open, FileAccess.Write, FileShare.Write)
sw = New BinaryWriter(fs)
End If
fs.Seek(LineWidth * Record, SeekOrigin.Begin)
sw.Write(b)
sw.Flush()
Open = False
Return False
Catch ex As Exception
Debug.Print(ex.Message)
Return True
End Try
End If
End Function
Public Sub CloseWriter()
sw.Close()
fs.Close()
fs.Dispose()
Open = True
End Sub
Private Function WriteRecord(ByVal Start As Integer, ByVal b() As Byte) As Boolean
Try
fs = New FileStream(fpath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Write)
sw = New BinaryWriter(fs)
fs.Seek(Start, SeekOrigin.Begin)
sw.Write(b)
sw.Flush()
sw.Close()
fs.Close()
fs.Dispose()
Return False
Catch ex As Exception
Debug.Print(ex.Message)
Return True
End Try
End Function ' check that file gets closed
#End Region
End Class