Create Binary stream of file

Posted by Blessyjees under C# category on | Points: 40 | Views : 2110
This code will give the binsry strem of the given file path
FileStream fileStream;
byte[] fileByte;
using (fileStream = File.OpenRead(filePath))
fileByte = new byte[fileStream.Length];
ReadWholeArray(fileStream, fileByte);
return fileByte;

private static void ReadWholeArray(Stream stream, byte[] data)
int offset = 0;
int remaining = data.Length;
while (remaining > 0)
int read = stream.Read(data, offset, remaining);
if (read <= 0)
throw new EndOfStreamException(String.Format("{0}:End of stream reached with {1} bytes left to read", "Generate binary stream", remaining));

remaining -= read;
offset += read;

Comments or Responses

Posted by: T.saravanan on: 10/12/2011 Level:Silver | Status: [Member] [MVP] | Points: 10

In your code what is the use of ReadWholeArray method kindly give a little bit explanation about your code.Its really useful to viewers.
Posted by: Blessyjees on: 10/17/2011 Level:Bronze | Status: [Member] | Points: 10

sorry, I missed to include the code of the function ReadWholeArray. I have updated the code. This function is used to read the stream as different group instead of full stream at a time.
Posted by: T.saravanan on: 10/17/2011 Level:Silver | Status: [Member] [MVP] | Points: 10

Login to post response