public static byte[] GetBytesFromFile(string file) |
{ |
try |
{ |
// check if this is a file name; |
if (file == null) |
return null; |
if (file.Trim().Length == 0) |
return null; |
// get the size of the buffer |
int bufferSize = (int)(new System.IO.FileInfo(file).Length); |
// check if the file has anything. |
if (bufferSize == 0) |
return null; |
// create the buffer with the proper size |
byte[] buffer = new byte[bufferSize]; |
// create the stream |
System.IO.FileStream fs = new System.IO.FileStream(file, System.IO.FileMode.Open); |
// create the reader of the stream |
System.IO.BinaryReader reader = new System.IO.BinaryReader(fs); |
// populate the buffer |
buffer = reader.ReadBytes(bufferSize); |
// Close the stream |
fs.Close(); |
fs.Dispose(); |
// close the reader |
reader.Close(); |
fs.Dispose(); |
return buffer; |
} |
catch (Exception ex) |
{ |
System.Windows.Forms.MessageBox.Show("Please Make sure that the file is valid or is not in use. Details:\r\n" + ex.Message); |
return null; |
} |
} |
Thursday, May 17, 2012
.NET C# Get Byte array from a file
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment