Solved

Read specific line using streamreader!

Posted on 2004-03-29
1
530 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 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Data organization issue 7 55
Header Font Size in Grid View 6 49
PowerShell: ForEach-Object Export to CSV 4 91
PowerShell:  Use of subproperties in a Select statement 7 68
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
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:…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

734 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