Export (0) Print
Expand All

MemoryStream.ReadByte Method

Reads a byte from the current stream.

[Visual Basic]
Overrides Public Function ReadByte() As Integer
[C#]
public override int ReadByte();
[C++]
public: int ReadByte();
[JScript]
public override function ReadByte() : int;

Return Value

The byte cast to a Int32, or -1 if the end of the stream has been reached.

Exceptions

Exception Type Condition
ObjectDisposedException The current stream instance is closed.

Remarks

For an example of creating a file and writing text to a file, see Writing Text to a File. For an example of reading text from a file, see Reading Text from a File. For an example of reading from and writing to a binary file, see Reading and Writing to a Newly Created Data File.

This method overrides ReadByte.

If the read operation is successful, the current position within the stream is advanced by one byte. If an exception occurs, the current position within the stream is unchanged.

Example

[Visual Basic, C#, C++] This code example is part of a larger example provided for the MemoryStream class.

[Visual Basic] 
' Read the remaining Bytes, Byte by Byte.
While(count < memStream.Length)
    byteArray(count) = _
        Convert.ToByte(memStream.ReadByte())
    count += 1
End While

[C#] 
// Read the remaining bytes, byte by byte.
while(count < memStream.Length)
{
    byteArray[count++] = 
        Convert.ToByte(memStream.ReadByte());
}

[C++] 
// Read the remaining bytes, byte by byte.
while(count < memStream->Length)
{
    byteArray[count++] = 
        Convert::ToByte(memStream->ReadByte());
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

MemoryStream Class | MemoryStream Members | System.IO Namespace | Working with I/O | Reading Text from a File | Writing Text to a File

Show:
© 2014 Microsoft