Writing test into files
We will continue to utilize the two different methods for VB.NET and older VB family members. For VB.NET, we will use the File
class from the System.IO
namespace to write text to files. For VBScript, VBA, and VB6, we will continue to use the FSO we used in the files and directories section.
The File
class also provides two methods that make writing text to a file in a single line easy. The WriteAllText
and AppendAllText
methods allow you to send the parameter to overwrite or append the file:
Imports System.IO Dim myFile As String = "C:\Path\File.txt" File.WriteAllText(myFile, "This text overwrites the file") File.AppendAllText(myFile, "This is appended text.")
In VB.NET, the StreamWriter
class can also write text to a file. First, you create a StreamWriter
instance, and then use its Write
or WriteLine
methods to write text:
Imports System.IO Dim myFile As String = "C:\Path\File.txt" Using writer As New StreamWriter...