Jan 22 2007

File to bytearray

Posted by admin under .NET

Useful function to binary read the content from a file into a byte array - which might be useful in scenarios for storing BLOBS in database etc



        public byte[] FileToArray(string sFilePath)
        {
            System.IO.FileStream fs = new System.IO.FileStream(sFilePath, 
                System.IO.FileMode.Open, System.IO.FileAccess.Read);
            System.IO.BinaryReader br = new System.IO.BinaryReader(fs);
            Byte[] bytes = br.ReadBytes((Int32)fs.Length);
            br.Close();
            fs.Close();
            return bytes;
        }


You can find sample usage if the function in this article on Insert blob into MySQL