Friday, June 18, 2010

Write Byte Array to File

How to write byte array to file ?
Ans :
//writeByteArrayToFile : This function write byte array to the file
        public bool writeByteArrayToFile(byte[] f_arData, string f_stFilePath)
        {
            //create response flag for notify the calling function about status
            //default value set to false
            bool l_blResponse = false;
            try
            {
                //Create FileStream instance in ReadWrite
                FileStream l_FileStream = new FileStream(f_stFilePath, FileMode.Create, FileAccess.ReadWrite);
                //Create BinaryReader Instance with open filestream instance
                BinaryWriter l_BinaryWriter = new BinaryWriter(l_FileStream);
                //Wriete Bytes to the file
                l_BinaryWriter.Write(f_arData);
                //Just need to close open streams
                l_BinaryWriter.Close();
                l_FileStream.Close();
                //set Response Falg to true
                l_blResponse = true;
            }
            catch (Exception ex)
            {
                //If we have any exception then just write message to console
                Console.WriteLine(ex.Message);
            }
            //return response to the calling funciton
            return l_blResponse;
        }

  

No comments:

Post a Comment