Home »
VB.Net »
VB.Net Programs
VB.Net program to write multiple bytes into a file
By Nidhi Last Updated : November 16, 2024
Write multiple bytes in a file in VB.Net
Here, we will use the WriteAllBytes() method of the File class to write multiple bytes to the specified file.
Program/Source Code:
The source code to write multiple bytes into a file is given below. The given program is compiled and executed successfully.
VB.Net code to write multiple bytes to the text file
'VB.Net program to write multiple bytes into a file.
Imports System.IO
Module Module1
Sub Main()
Try
Dim buff() As Byte = {1, 2, 3, 4, 5}
File.WriteAllBytes("Sample.txt", buff)
Console.WriteLine("Data written successfully")
Catch ex As FileNotFoundException
Console.WriteLine("File does not exist")
End Try
End Sub
End Module
Output
Data written successfully
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, which is initialized with some byte values.
File.WriteAllBytes("Sample.txt", buff)
Console.WriteLine("Data written successfully")
In the above code, we write multiple bytes into the "sample.txt" file and printed the "Data written successfully" message on the console screen.
VB.Net File Handling Programs »