Write text in a file in VB.NET

This blog defines the StreamWriter class to Write the text to a file in VB.NET.

StreamWriter class

Using StreamWriter class to write  text in file. The following code uses the StreamWriter class to open, to write to, and to close the text file. Similar to StreamReader, you can pass the path name of a text file to the StreamWriter constructor to open the file automatically.

The WriteLine method - This method is used to write a complete line of text to the text file.

For example

Imports System.IO

Module Module1

    Sub Main()

       Dim objStreamWriter As StreamWriter

        objStreamWriter = New StreamWriter("C:\roh1.txt")

        objStreamWriter.WriteLine("Hello World")

        objStreamWriter.WriteLine("Rohatash Kumar")

        objStreamWriter.Close()

    End Sub

End Module

Now run the above code and test the file with String Hello word and Rohatash Kumar at the location C:\roh1.txt.