1 Replies - 10998 Views - Last Post: 02 July 2008 - 11:09 AM Rate Topic: -----

#1 maile  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 02-July 08

Go to a particular line in a Text File using VB.Net

Post icon  Posted 02 July 2008 - 10:57 AM

Guys, how do i navigate to a particular line number when reading a text file in VB.net? The code i use reads through the file without stoping at desired lines. This is the code:

Imports System
Imports System.IO

Class Test
	Public Shared Sub Main()
		Try
			' Create an instance of StreamReader to read from a file.
			Using sr As StreamReader = New StreamReader("TestFile.txt")
				Dim line As String
				' Read and display the lines from the file until the end 
				' of the file is reached.
				Do
					line = sr.ReadLine()
					Console.WriteLine(Line)
				Loop Until line Is Nothing
				sr.Close()
			End Using
		Catch E As Exception
			' Let the user know what went wrong.
			Console.WriteLine("The file could not be read:")
			Console.WriteLine(E.Message)
		End Try
	End Sub
End Class 



Mod Edit: Please use code tags when posting your code. Code tags are used like so => :code:

Thanks,
PsychoCoder :)

Is This A Good Question/Topic? 0
  • +

Replies To: Go to a particular line in a Text File using VB.Net

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1642
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Go to a particular line in a Text File using VB.Net

Posted 02 July 2008 - 11:09 AM

Here is a method from a tutorial on System.IO here in Dream.In.Code. Look through this and it should help you with your problem


''' <summary>
    ''' Method to read a specified line in a text file
    ''' </summary>
    ''' <param name="line">Line number to read</param>
    ''' <returns></returns>
    Public Function ReadSpecifiedLine(ByVal line As Integer) As String
        'create a variable to hold the contents of the file
        Dim contents As String = String.Empty
        'create a variable to hold our line contents
        Dim lineText As String = String.Empty
        ' always use a try...catch to deal
        ' with any exceptions that may occur
        Try
          
            'thanks for the idea from RodgerB at </dream.in.code>
            Using lineByLine As New IO.StreamReader(_fileName)
                Dim lineCount As Integer = 0
                While Not lineByLine.EndOfStream
                    lineByLine.ReadLine()
                    If lineCount = line Then
                        lineText = lineByLine.ReadLine()
                    End If
                    lineCount += 1
                End While
            End Using
        Catch ex As FileNotFoundException
            lineText = String.Empty
            _returnMessage = ex.Message
        Catch ex As Exception
            ' deal with any errors
            _returnMessage = ex.Message
        End Try
        Return lineText
    End Function


Was This Post Helpful? 1

Page 1 of 1