Write a byte array to a file in CSharp

Description

The following code shows how to write a byte array to a file.

Example


using System;//from w  ww. j a  va 2  s . c om
using System.Collections.Generic;
using System.Text;

public class MainClass
{
        public static void Main(){
            WriteFile("test.txt", new byte[]{67,66,68});
        }

        public static int WriteFile(String strFilePath, byte[] bContent)
        {
            System.IO.FileStream fs = new System.IO.FileStream(strFilePath, System.IO.FileMode.Append, System.IO.FileAccess.Write, System.IO.FileShare.ReadWrite);
            System.IO.BinaryWriter bw = new System.IO.BinaryWriter(fs);
            try
            {
                bw.Write(bContent);
            }
            finally
            {
                bw.Close();
                fs.Close();
            }
            return 0;
        }
}




















Home »
  C# Tutorial »
    Data Types »




C# Data Types
Bool
Byte
Char
Decimal
Double
Float
Integer
Long
Short
String
C# Array
Array Example
Byte Array
C# Standard Data Type Format
BigInteger
Complex
Currency
DateTime
DateTimeOffset
DateTime Format Parse Convert
TimeSpan
TimeZone
Enum
Null
tuple
var