Solved

Read specific line using streamreader!

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
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…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

690 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