BinaryWriter.Write Method (String)
Writes a length-prefixed string to this stream in the current encoding of the BinaryWriter, and advances the current position of the stream in accordance with the encoding used and the specific characters being written to the stream.
Assembly: mscorlib (in mscorlib.dll)
Length-prefixed means that this method first writes the length of the string, in bytes, when encoded with the BinaryWriter instance's current encoding to the stream. This value is written as an unsigned integer. This method then writes that many bytes to the stream.
For example, the string “A” has a length of 1, but when encoded with UTF-16; the length is 2 bytes, so the value written in the prefix is 2, and 3 bytes are written to the stream, including the prefix.
For a list of common I/O tasks, see Common I/O Tasks.
The following code example demonstrates how to store and retrieve application settings in a file.
Imports System.IO Module Module1 Const fileName As String = "AppSettings.dat" Sub Main() WriteDefaultValues() DisplayValues() End Sub Sub WriteDefaultValues() Using writer As BinaryWriter = New BinaryWriter(File.Open(fileName, FileMode.Create)) writer.Write(1.25F) writer.Write("c:\Temp") writer.Write(10) writer.Write(True) End Using End Sub Sub DisplayValues() Dim aspectRatio As Single Dim tempDirectory As String Dim autoSaveTime As Integer Dim showStatusBar As Boolean If (File.Exists(fileName)) Then Using reader As BinaryReader = New BinaryReader(File.Open(fileName, FileMode.Open)) aspectRatio = reader.ReadSingle() tempDirectory = reader.ReadString() autoSaveTime = reader.ReadInt32() showStatusBar = reader.ReadBoolean() End Using Console.WriteLine("Aspect ratio set to: " & aspectRatio) Console.WriteLine("Temp directory is: " & tempDirectory) Console.WriteLine("Auto save time set to: " & autoSaveTime) Console.WriteLine("Show status bar: " & showStatusBar) End If End Sub End Module
.NET FrameworkSupported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1
.NET Framework Client ProfileSupported in: 4, 3.5 SP1
XNA FrameworkSupported in: 3.0, 2.0, 1.0
Portable Class LibrarySupported in: Portable Class Library
Supported in: Windows Phone 8.1
Supported in: Windows Phone Silverlight 8.1
Supported in: Windows Phone Silverlight 8