Home »
VB.Net »
VB.Net Programs
VB.Net program to write data into a text file
Here, we are going to learn how to write data into a text file in VB.Net?
Submitted by Nidhi, on January 08, 2021 [Last updated : March 08, 2023]
Writing data into a text file in VB.Net
Here, we will read text data from the user and write it into a text file using the WriteAllText() method of the File class.
Program/Source Code:
The source code to write data into a text file is given below. The given program is compiled and executed successfully.
VB.Net code to write data into a text file
'VB.Net program to write text into a file.
Imports System.IO
Module Module1
Sub Main()
Dim str As String = Nothing
Console.WriteLine("Enter data to Write into File : ")
str = Console.ReadLine()
File.WriteAllText("sample.txt", str)
Console.WriteLine("File Creation Done")
End Sub
End Module
Output
Enter data to Write into File :
Hello, how are you.
File Creation Done
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. And, we created a local variable str of string type. After that read the value of the str variable from the user and then write data into the "sample.txt" file using WriteAllText() method of the File class.
VB.Net File Handling Programs »