Open File and read file to byte array - CSharp System

CSharp examples for System:Byte Array

Description

Open File and read file to byte array

Demo Code

/*//from  ww  w .  jav a2  s  . c om
Copyright ?2009 John Allberg

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/
using System.IO;
using System.Security.Cryptography;
using System.Diagnostics;
using System;

public class Main{
    public static byte[] OpenFile(string filename)
      {
         System.IO.MemoryStream mem = new MemoryStream();
         System.IO.FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read);
         
         byte[] read = new byte[256];
         int count = fs.Read(read, 0, 256);

         while (count > 0) 
         {
            mem.Write(read,0,count);

            count = fs.Read(read, 0, 256);
         }
         fs.Flush();
         fs.Close();
         return mem.ToArray();
      }
}

Related Tutorials