VB.Net program to read all bytes from a file

Here, we are going to learn how to read all bytes from a file in VB.Net?
Submitted by Nidhi, on January 16, 2021 [Last updated : March 08, 2023]

Reading all bytes from a file in VB.Net

Here, we will use the ReadAllBytes() method of the File class to read all byte values from the specified file.

Program/Source Code:

The source code to read all bytes from a file is given below. The given program is compiled and executed successfully.

VB.Net code to read all bytes from a file

'VB.Net program to read all bytes from a file.

Imports System.IO

Module Module1
    Sub Main()
        Try
            Dim buff(5) As Byte

            buff = File.ReadAllBytes("Sample.txt")
            Console.WriteLine("Data of file Sample.txt :")

            For i = 0 To buff.Length - 1 Step 1
                Console.Write(buff(i) & " ")
            Next

            Console.WriteLine()

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

Output

Data of file Sample.txt :
1 2 3 4 5
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 bytes for 5-byte elements.

buff = File.ReadAllBytes("Sample.txt")
Console.WriteLine("Data of file Sample.txt :")

For i = 0 To buff.Length - 1 Step 1
    Console.Write(buff(i) & " ")
Next

In the above code, we read all bytes from the "sample.txt" file and printed all values on the console screen.

VB.Net File Handling Programs »






Comments and Discussions!

Load comments ↻






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