turrier.fr

Source : ct|22.09.09

< Tutorials Computer, Multimedia, Chinese

Read and write binary files in Csharp and VB

The languages csharp and VB offer various classes and methods to manipulate easily binaries files.
The BinaryReader Class ( Namespace System.IO , Assembly mscorlib.dll) allows to read files byte by byte. The method ReadByte (class BinaryReader ) can read the next byte, starting from the current position and advancing the current position of a byte. The BinaryWriter Class (System.IO Namespace, Assembly mscorlib.dll) allows to write files byte by byte. The Write method of the the BinaryWriter class can write a byte to the unsigned current position and advance the current position of a byte. The objects of the FileStream class can create, open, close, read and write files with which they are associated. These objects allows to access at any byte of the files by using the Seek method. The Seek method sets the current position as that of any byte of the file.

In this tutorial,we will, with the Framework .net 2.0 and the IDE SharpDevelop 2.2 :
1) Create a sample program
2) Check the results.

Building the sample program

With SharpDevelop, create a C # console type program called EssaiFichierBinaire " eg.

Console application

Place the following source code in Program.cs :

using System;
using System.IO;
namespace TestFichierBin
{
class Program
{
public static void Main(string[] args)
{
string nom="monfichier.dat"; byte i;
BinaryReader br = null;
BinaryWriter bw = null;
FileStream fs = null;
//Ecriture d'octets dans le fichier
bw = new BinaryWriter(File.Create(nom));
i=0; bw.Write(i);i=1; bw.Write(i); i=2; bw.Write(i);
bw.Close();
fs = File.Open(nom, FileMode.Open);
//Lecture du fichier à partir de la position 0
fs.Seek(0, SeekOrigin.Begin);
br = new BinaryReader(fs);
while (fs.Position < fs.Length) Console.Write(br.ReadByte());
Console.WriteLine("\n-----------------");
//fin du programme
br.Close();
bw.Close();
Console.ReadKey(true);
}
}
}

Build the solution (F8) and then run the program (F5).

In VB, the equivalent code is :

Imports System
Imports System.IO
Module Program
Sub Main()
Dim nom as String ="monfichier.dat" : Dim i as Byte
Dim br as BinaryReader
Dim bw as BinaryWriter
Dim fs as FileStream
'Ecriture d'octets dans le fichier
bw = new BinaryWriter(File.Create(nom))
i=0 : bw.Write(i):i=1: bw.Write(i): i=2: bw.Write(i)
bw.Close()
fs = File.Open(nom, FileMode.Open)
'Lecture du fichier à partir de la position 0
fs.Seek(0, SeekOrigin.Begin)
br = new BinaryReader(fs)
While (fs.Position < fs.Length)
Console.Write(br.ReadByte())
End While
Console.WriteLine(chr(10) +"-----------------")
'fin du programme
br.Close()
bw.Close()
Console.ReadKey(true)
End Sub
End Module

Verify results

This program allows to:
- Create a binary file named "monfichier.dat;
- Write the bytes 00, 01 and 02 at locations 0, 1 and 2 of this file;
- Open file and read it;
- Display its contents in the window of the Windows console.

Note that the instruction Console.ReadKey (true), located at the end of the program,prevents that the console window closes too quickly.

Programm executed

The file "monfichier.dat" is in the program directory.

Binary file

By opening the file with a hex editor, we find the expected content.

Content of the binary file


Valid XHTML 1.0 Transitional

© http://turrier.fr (2007)