Implements a TextWriter for writing characters to a stream in a particular encoding.
Assembly: mscorlib (in mscorlib.dll)
is designed for character output in a particular encoding, whereas classes derived from Stream are designed for byte input and output.
defaults to using an instance of UTF8Encoding unless specified otherwise. This instance of UTF8Encoding is constructed without a byte order mark (BOM), so its GetPreamble method returns an empty byte array. To create a using UTF-8 encoding and a BOM, consider using a constructor that specifies encoding, such as StreamWriter(String, Boolean, Encoding).
By default, a StreamWriter is not thread safe. See TextWriter.Synchronized for a thread-safe wrapper.
For a list of common I/O tasks, see Common I/O Tasks.
The following example shows how to use a object to write a file that lists the directories on drive C, and then use a StreamReader object to read and display each directory name. It is good practice to use these objects in a using statement so that the unmanaged resources are correctly disposed. The using statement automatically calls Dispose on the object when the code that is using it has completed.
Imports System.IO Class Program Shared Sub Main() ' Get the directories currently on the C drive. Dim cDirs As DirectoryInfo() = New DirectoryInfo("c:\").GetDirectories() ' Write each directory name to a file. Using sw As StreamWriter = New StreamWriter("CDriveDirs.txt") For Each Dir As DirectoryInfo In cDirs sw.WriteLine(Dir.Name) Next End Using 'Read and show each line from the file. Dim line As String = "" Using sr As StreamReader = New StreamReader("CDriveDirs.txt") Do line = sr.ReadLine() Console.WriteLine(line) Loop Until line Is Nothing End Using End Sub End Class
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.