C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » File Operations »

Convert a File into a Byte array


Posted Date: 01 Jan 2008    Resource Type: Code Snippets    Category: File Operations
Author: KarthickMember Level: Gold    
Rating: 1 out of 5Points: 7



The code sample is a method to convert file into byte array.

Sometimes we require to convert file into byte to save it in a database or send them to other systems using Remoting.

The code snippet consists of a function StreamFile() that takes the file's name as input and gives back an array of bytes. The steps involved in doing so are explained in the comments.




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
fs.Read(ImageData,0,System.Convert.ToInt32(fs.Length));

//Close the File Stream
fs.Close();
return ImageData; //return the byte data
}




Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Transfer files with Remoting  .  Transfer File through Remoting  .  Save Files in a Database  .  Get File in the form of a Byte Array  .  Convert File to Byte Array  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Retrieving Sub-directories
Previous Resource: Delete all the Temporary Internet files
Return to Discussion Resource Index
Post New Resource
Category: File Operations


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use