Completely Free .NET word API

Resources » Code Snippets » File Operations

convert file into byte array

Updated: Category: File Operations
Author: Member Level: GoldPoints: 10

In this article author explain about how convert file into byte array

This function is used to convert file into byte array. some time we require to convert file into byte to save into database or send to other system using remoting.

private byte [] StreamFile(string filename)
FileStream fs = new FileStream(filename, FileMode.Open,FileAccess.Read);

// Create a byte array of file stream length
byte[] ImageData = new byte[fs.Length];

//Read block of bytes from stream into the byte array

//Close the File Stream
return ImageData; //return the byte data

Did you like this resource? Share it with your friends and show your love!

Responses to "convert file into byte array"
Guest Author: Aaron   12 Jul 2012
You can't guarantee that the entire file will be read with just one call to fs.Read. You need to check if the returned value (bytes read) equals fs.Length. If not you need to advance the position and read the rest.


Post Comment:

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)

    Type the numbers and letters shown on the left.

    Submit Article     Return to Article Index
    Subscribe to Subscribers
    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Talk to Webmaster Tony John
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India