Solved

Read specific line using streamreader!

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

The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

Question has a verified solution.

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

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

809 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