Welcome to Dream.In.Code
Getting VB.NET Help is Easy!

Join 105,755 VB.NET Programmers for FREE! Ask your question and get quick answers from experts. There are 1,760 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



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

 
Reply to this topicStart new topic

Go to a particular line in a Text File using VB.Net, Manipulating text files in VB.NET

maile
post 2 Jul, 2008 - 10:57 AM
Post #1


New D.I.C Head

*
Joined: 2 Jul, 2008
Posts: 1

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:

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.gif

Thanks,
PsychoCoder smile.gif
User is offlineProfile CardPM

Go to the top of the page


PsychoCoder
post 2 Jul, 2008 - 11:09 AM
Post #2


DIC.Rules == true;

Group Icon
Joined: 26 Jul, 2007
Posts: 7,028



Thanked 47 times

Dream Kudos: 7600

Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, GDI

My Contributions


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


vb

''' <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
User is online!Profile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 8/21/08 12:23PM

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month