Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

FileStream.WriteByte Method

Writes a byte to the current position in the file stream.

Namespace:  System.IO
Assembly:  mscorlib (in mscorlib.dll)

'Declaration
Public Overrides Sub WriteByte ( _
	value As Byte _
)

Parameters

value
Type: System.Byte

A byte to write to the stream.

ExceptionCondition
ObjectDisposedException

The stream is closed.

NotSupportedException

The stream does not support writing.

This method overrides WriteByte.

Use WriteByte to write a byte to a FileStream efficiently. If the stream is closed or not writable, an exception will be thrown.

NoteNote

Use the CanWrite property to determine whether the current instance supports writing. For additional information, see CanWrite.

Notes to Implementers

The default implementation on Stream creates a new single-byte array and then calls Write. While this is formally correct, it is inefficient. Any stream with an internal buffer should override this method and provide a much more efficient version that reads the buffer directly, avoiding the extra array allocation on every call.

For a list of common file and directory operations, see Common I/O Tasks.

The following code example shows how to write data to a file, byte by byte, and then verify that the data was written correctly.

Imports Microsoft.VisualBasic
Imports System
Imports System.IO
Imports System.Text

Class FStream

    Shared Sub Main()

        Const fileName As String = "Test#@@#.dat" 

        ' Create random data to write to the file. 
        Dim dataArray(100000) As Byte 
        Dim randomGenerator As New Random()
        randomGenerator.NextBytes(dataArray)

        Dim fileStream As FileStream = _
            new FileStream(fileName, FileMode.Create)
        Try 

            ' Write the data to the file, byte by byte. 
            For i As Integer = 0 To dataArray.Length - 1
                fileStream.WriteByte(dataArray(i))
            Next i

            ' Set the stream position to the beginning of the stream.
            fileStream.Seek(0, SeekOrigin.Begin)

            ' Read and verify the data. 
            For i As Integer = 0 To _
                CType(fileStream.Length, Integer) - 1

                If dataArray(i) <> fileStream.ReadByte() Then
                    Console.WriteLine("Error writing data.")
                    Return 
                End If 
            Next i
            Console.WriteLine("The data was written to {0} " & _
                "and verified.", fileStream.Name)
        Finally
            fileStream.Close()
        End Try 

    End Sub 
End Class

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

XNA Framework

Supported in: 3.0, 2.0, 1.0

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone Silverlight 8.1, Windows Phone Silverlight 8
Show:
© 2015 Microsoft