Assembly: mscorlib (in mscorlib.dll)
'Declaration Public MustOverride Function Read ( _ <InAttribute> <OutAttribute> buffer As Byte(), _ offset As Integer, _ count As Integer _ ) As Integer 'Usage Dim instance As Stream Dim buffer As Byte() Dim offset As Integer Dim count As Integer Dim returnValue As Integer returnValue = instance.Read(buffer, offset, count)
public abstract int Read ( /** @attribute InAttribute() */ /** @attribute OutAttribute() */ byte buffer, int offset, int count )
An array of bytes. When this method returns, the buffer contains the specified byte array with the values between offset and (offset + count - 1) replaced by the bytes read from the current source.
The zero-based byte offset in buffer at which to begin storing the data read from the current stream.
The maximum number of bytes to be read from the current stream.
Return ValueThe total number of bytes read into the buffer. This can be less than the number of bytes requested if that many bytes are not currently available, or zero (0) if the end of the stream has been reached.
The sum of offset and count is larger than the buffer length.
buffer is a null reference (Nothing in Visual Basic).
offset or count is negative.
An I/O error occurs.
The stream does not support reading.
Methods were called after the stream was closed.
For an example of creating a file and writing text to a file, see How to: Write Text to a File. For an example of reading text from a file, see How to: Read Text from a File. For an example of reading from and writing to a binary file, see How to: Read and Write to a Newly Created Data File.
Use the CanRead property to determine whether the current instance supports reading.
Implementations of this method read a maximum of count bytes from the current stream and store them in buffer beginning at offset. The current position within the stream is advanced by the number of bytes read; however, if an exception occurs, the current position within the stream remains unchanged. Implementations return the number of bytes read. The return value is zero only if the position is currently at the end of the stream. The implementation will block until at least one byte of data can be read, in the event that no data is available. Read returns 0 only when there is no more data in the stream and no more is expected (such as a closed socket or end of file). An implementation is free to return fewer bytes than requested even if the end of the stream has not been reached.
Use BinaryReader for reading primitive data types.
The following example shows how to use Read to read a block of data.
Imports System Imports System.IO Imports Microsoft.VisualBasic Public Class Block Public Shared Sub Main() Dim s As New MemoryStream() Dim i As Integer For i = 0 To 99 s.WriteByte(CByte(i)) Next i s.Position = 0 ' Now read in s into a byte buffer. Dim bytes(s.Length) As Byte Dim numBytesToRead As Integer = CInt(s.Length) Dim numBytesRead As Integer = 0 While numBytesToRead > 0 ' Read can return anything from 0 to numBytesToRead. Dim n As Integer = s.Read(bytes, numBytesRead, numBytesToRead) ' The end of the file has been reached. If n = 0 Then Exit While End If numBytesRead += n numBytesToRead -= n End While s.Close() ' numBytesToRead should be 0 now, and numBytesRead should ' equal 100. Console.WriteLine("number of bytes read: " & numBytesRead.ToString()) End Sub End Class
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.