×

VB.Net Programs

VB.Net program to read all lines of a text file

By Nidhi Last Updated : November 16, 2024

Read all lines of a file in VB.Net

Here, we will use the ReadAllLines() method of the File class to read all lines from the text file.

Program/Source Code:

The source code to read all lines of a text file is given below. The given program is compiled and executed successfully.

VB.Net code to read all lines of a text file

'VB.Net program to read all lines of a text file.

Imports System.IO

Module Module1
    Sub Main()
        Try
            Dim line(5) As String

            line = File.ReadAllLines("sample.txt")

            For i = 0 To line.Length - 1 Step 1
                Console.WriteLine(line(i))
            Next

        Catch ex As FileNotFoundException
            Console.WriteLine("File does not exist")
        End Try
    End Sub
End Module

Output

Person1: hello, how are you?
Person2: I am fine, what about you?
Person1: I am also fine, you are looking handsome.
Person2: Thanks.
Press any key to continue . . .

Explanation

In the above program, we created a module Module1 that contains the Main() function. The Main() function is the entry point for the program, here, we created an array of strings that may contain 5 string elements.

line = File.ReadAllLines("sample.txt")

In the above code, we read all lines from the "sample.txt" file and return the array of string, which is assigned to the line and then we printed the data on the console screen.

VB.Net File Handling Programs »


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.