Home »
VB.Net »
VB.Net Programs
VB.Net program to read all bytes from a file
By Nidhi Last Updated : November 16, 2024
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 »