?
Solved

Read specific line using streamreader!

Posted on 2004-03-29
1
Medium Priority
?
533 Views
Last Modified: 2012-05-04
How to read specific line using streamreader in vb.net. For example I need to read line # 10 in test.txt  How can i done it???

Also, i have i list ListBox1
what i want to make is when my ListBox1.selectedindex = 0 then read line #0 from text file,,, if selectedindex =1 then read line#1 and so on,,,, How can it be done,,,
0
Comment
Question by:g00r00
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 12

Accepted Solution

by:
farsight earned 80 total points
ID: 10711065
   Private Sub TryGetLine2()
        Dim filename As String = "Schema.ini"
        ' Show lines 2, 3, and 4.
        MessageBox.Show(GetLine2(filename, 2))
        MessageBox.Show(GetLine2(filename, 3))
        MessageBox.Show(GetLine2(filename, 4))
        'MessageBox.Show(GetLine2(filename, -73))    'Should throw exception.
    End Sub

    Private Function GetLine2(ByVal filename As String, ByVal lineNumber As Integer) As String
        Dim sr As System.IO.StreamReader
        Dim content As String
        Dim currentLineNumber As Integer = 0   ' Initialized to the number for the first line.
        Try
            sr = New System.IO.StreamReader(filename)
            Do While sr.Peek <> -1
                content = sr.ReadLine()
                If currentLineNumber = lineNumber Then
                    Return content
                End If
                currentLineNumber += 1
            Loop
        Catch
        Finally
            sr.Close()
        End Try
        Throw New ApplicationException("GetLine2: Line not found.")
    End Function
0

Featured Post

Is Your Team Achieving Their Full Potential?

74% of employees feel they are not achieving their full potential. With Linux Academy, not only will you strengthen your team's core competencies but also their knowledge of of the newest IT topics.

With new material every week, we'll make sure that you stay ahead of the game.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question