How to read a text file in VB.NET

This code sample shows how to read a text file in VB.NET.
  • 6556
 

We can use StreamReader to read text files. The following code snippet reads a file and writes on the console line by line.

Module Module1

 

    Sub Main()

 

 

        Dim fileName As String = "C:\Mahesh\McTextFile.txt"

        Using reader As New System.IO.StreamReader(fileName)

            While Not reader.ReadLine() Is Nothing

                Console.WriteLine(reader.ReadLine())

            End While

        End Using

 

        Try

            Using writer As New System.IO.StreamWriter(fileName, True)

                writer.WriteLine("New text")

                writer.WriteLine("New text line goes here")

            End Using

        Catch ex As Exception

 

        End Try

 

        Console.ReadKey()

 

    End Sub

 

End Module

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.