I need to find out how to display the last 128 characters of a text document. I've tried using StreamReader to get the whole file but I only need the last 128 characters. Here is my code so far:
<VB.NET 2005>
Private Sub Button12_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button12.Click If ListBox1.Text = "" ThenMessageBox.Show(
"No File Selected", "Error") Exit Sub End If Dim sr As New System.IO.StreamReader(ListBox1.Text) Dim WholeFile As String = sr.ReadToEnd() Dim LastDigits As String = "" Dim Index As Integer If WholeFile.Length > 128 Then For Index = WholeFile.Length - 128 To WholeFile.Length - 1LastDigits = LastDigits & WholeFile(Index)
NextMessageBox.Show(LastDigits,
"Test") End If End Sub</VB.NET 2005>
It doesn't display the data correctly, only the first few words and it doesn't display several blankspaces " " as is in the file.
Can somebody please help... Thanks in advance.

StreamReader problem
Jim Kelly
Something like the following should work:
Dim sr As New System.IO.StreamReader("YourFile.mp3")
Dim br As New System.IO.BinaryReader(sr.BaseStream)
sr.BaseStream.Seek(-128, IO.SeekOrigin.End)
Dim b() As Byte = br.ReadBytes(128)
If GetString(b, 0, 3) <> "TAG" Then
Throw New Exception("No ID3v1 tag found")
End If
Dim Title As String = GetString(b, 3, 30)
Dim Artist As String = GetString(b, 33, 30)
Dim Album As String = GetString(b, 63, 30)
Dim Year As String = GetString(b, 93, 4)
Dim Comment As String = GetString(b, 97, 30)
Dim Genre As Byte = b(127)
(don't forget to close sr and br)
Private Function GetString(ByVal b() As Byte, ByVal StartOffset As Integer, ByVal MaxLen As Integer) As String
Dim res As String = ""
For i As Integer = StartOffset To StartOffset + MaxLen - 1
If b(i) = 0 Then
Exit For
Else
res &= Chr(CInt(b(i)))
End If
Next i
Return res.Trim
End Function
'//mdb
Bill Nortman
Thanks,
It's an MP3 file and I want to retrieve the ID3v1 tag which is in the last 128 digits. This brings up the same string in a messagebox as my code. This is a lot shorter than my code though :)
It only brings up "TAG{Title}" then nothing...
Do I need to convert the string or something
Thanks again,
cin_ful
You don't specify the exact file format, but something like the following should work for pure text files:
Dim sr As New System.IO.StreamReader("YourFileNameHere")sr.BaseStream.Seek(-128, IO.SeekOrigin.End)
Dim LastDigits As String = sr.ReadToEnd()
'//mdb
SteveMeyns
mdb,
Thanks for the help. That works like a dream. Cheers,