Solved

Read specific line using streamreader!

Posted on 2004-03-29
1
524 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
1 Comment
 
LVL 12

Accepted Solution

by:
farsight earned 20 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
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 …
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

746 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now