Đọc ghi dữ liệu Binary trong C#

Mã nguồn đọc ghi dữ liệu binary trong C#, ví dụ dùng để copy một file ảnh dạng binary qua file ảnh mới

class BinaryData
{

private int bufferSize = 4096;

public void Run()
{

//Open source file to read
FileStream streamRead = new FileStream(@”e:\pictures\DSCN0154.jpg”,FileMode.Open,FileAccess.Read);
BinaryReader reader = new BinaryReader(streamRead);

//Open destination file to write
FileStream streamWrite = new FileStream(@”c:\DSCN0154.jpg”,FileMode.OpenOrCreate,FileAccess.Write);
BinaryWriter writer = new BinaryWriter(streamWrite);

byte [] buffer = new byte[bufferSize];
int byteRead;
while ((byteRead = reader.Read(buffer, 0, bufferSize)) > 0)
{
writer.Write(buffer, 0, byteRead);
}
streamRead.Close();
streamWrite.Close();

}

}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s