C# BinaryReader ReadBytes

Description

BinaryReader ReadBytes Reads the specified number of bytes from the current stream into a byte array and advances the current position by that number of bytes.

Syntax

BinaryReader.ReadBytes has the following syntax.


public virtual byte[] ReadBytes(
  int count
)

Parameters

BinaryReader.ReadBytes has the following parameters.

  • count - The number of bytes to read. This value must be 0 or a non-negative number or an exception will occur.

Returns

BinaryReader.ReadBytes method returns

Example


using System;/* w  ww.j  ava 2s.co m*/
using System.IO;

class ConsoleApplication
{
    const string fileName = "data.dat";

    static void Main()
    {

        using (BinaryWriter writer = new BinaryWriter(File.Open(fileName, FileMode.Create)))
        {
            writer.Write(true);
        }

        using (BinaryReader reader = new BinaryReader(File.Open(fileName, FileMode.Open)))
        {
            byte[] dataArray = new byte[100];
            dataArray = reader.ReadBytes(100);
        }
    }
}




















Home »
  C# Tutorial »
    System.IO »




BinaryReader
BinaryWriter
Directory
DirectoryInfo
DriveInfo
File
FileInfo
FileStream
MemoryStream
Path
StreamReader
StreamWriter
StringReader
StringWriter