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


            End While

        End Using



            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




    End Sub


End Module


More Articles

© 2020 DotNetHeaven. All rights reserved.