Mega Code Archive

 
Categories / C# / File Stream
 

Read all the content from a file as byte array

//http://tinyerp.codeplex.com/ //GNU Library General Public License (LGPL) //----------------------------------------------------------------------- // <copyright file="SysUtil.cs" company="Pyramid Consulting"> //     Copyright (c) Pyramid Consulting. All rights reserved. // </copyright> //----------------------------------------------------------------------- using System; using System.Collections.Generic; using System.Text; namespace Bamboo.Core.Common {     public class SysUtil     {         /// <summary>         /// read all the content from a file as byte array         /// </summary>         /// <param name="strFilePath">source file path</param>         /// <returns>dest byte array on succced</returns>         public static byte[] ReadFileAsBytes(String strFilePath)         {             System.IO.FileStream fs = new System.IO.FileStream(strFilePath, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite);             System.IO.BinaryReader br = new System.IO.BinaryReader(fs);             byte[] baResult = null;             try             {                 baResult = new byte[fs.Length];                 br.Read(baResult, 0, baResult.Length);             }             finally             {                 br.Close();                 fs.Close();             }             return baResult;         }     } }