Visual Basic 2010 (Console) Guide
Writing Text Files

Writing to a text file is a very similar process to reading. The main difference is that sometimes we will want to overwrite the contents of a file, sometimes we will want to append information to it.

Append All Text/Write All Text

Dim path As String = "C:\q.txt"
Dim strText As String = "Some text to add to a file"
File.AppendAllText(path, strText)
Console.WriteLine("Appended text to file")

If the file specified in the first line does not exist, it will be created. The alternative to this is to use the WriteAllText() method.

Writing A Line At A Time

In the same way that we can use a streamreader to read the file, we can use a streamwriter to write one.

Dim path As String = "C:\q.txt"
Dim sw As StreamWriter = New StreamWriter(path)
Dim input As String
Console.Write("Enter a name for the file or end to quit")
input = Console.ReadLine()
While (input <> "end")
   Console.Write("Enter another name for the file or end to quit")
   input = Console.ReadLine()
End While
Console.WriteLine("Finished writing the file")

There are quite a few additional techniques that you might use for writing to text files. The MSDN for Visual Basic has fairly good explanations and examples on the topic.