Byte Array To File - CSharp System

CSharp examples for System:Byte Array

Description

Byte Array To File

Demo Code


using System.Threading.Tasks;
using System.Text;
using System.Net;
using System.Linq;
using System.Collections.Generic;
using System;/* w w  w .j ava  2  s  .c o m*/

public class Main{
        public static bool ByteArrayToFile(string fileName, byte[] data)
       {
          try
          {
             // Open file for reading
             System.IO.FileStream _FileStream =
                new System.IO.FileStream(fileName, System.IO.FileMode.Create,
                                         System.IO.FileAccess.Write);
             // Writes a block of bytes to this stream using data from
             // a byte array.
             _FileStream.Write(data, 0, data.Length);

             // close file stream
             _FileStream.Close();

             return true;
          }
          catch (Exception _Exception)
          {
             // Error
             Console.WriteLine("Exception caught in process: {0}",
                               _Exception.ToString());
          }

          // error occured, return false
          return false;
       }
}

Related Tutorials