Read a binary file
8
Class method to read a binary file and copy it to a byte array.
/// <summary>
/// Read a binary file
/// </summary>
/// <param name="filePath">Absolute Path of file to read from</param>
/// <example>byte[] myFile = myFileObj.ReadBinaryFile(@"C:\log.txt");</example>
/// <returns>Byte contents of the file</returns>
public byte[] ReadBinaryFile(string filePath)
{
byte[] file = null;
FileStream fs = null;
BufferedStream bufs = null;
BinaryReader bs = null;
try
{
fs = new FileStream(filePath,FileMode.Open, FileAccess.Read);
bufs = new BufferedStream(fs);
bs = new BinaryReader(bufs);
long lFile = bs.BaseStream.Length;
if (lFile>Int32.MaxValue) // if file > 2GB
{
throw new Exception("File too long");
}
file = bs.ReadBytes((int)lFile);
this.errMsg="";
}
catch (Exception e)
{
System.Diagnostics.Debug.WriteLine(e.Source);
System.Diagnostics.Debug.WriteLine(e.Message);
System.Diagnostics.Debug.WriteLine(e.StackTrace);
}
finally
{
if (bs!= null) bs.Close();
if (bufs!=null) bufs.Close();
if (fs!=null) fs.Close();
}
return file;
}






There are currently no comments for this snippet.