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;
        }

  

Read Byte Array From File

 //ReadByteArrayFromFile : This function read file from FilePath
        public byte[] ReadByteArrayFromFile(string f_stFilePath)
        {
            //Create Data array
            byte[] l_arData = null;
            //Create FileStream instance in Read mode
            FileStream l_FileStream = new FileStream(f_stFilePath, FileMode.Open, FileAccess.Read);
            //Create BinaryReader Instance
            BinaryReader l_BinaryReader = new BinaryReader(l_FileStream);
            //Calculate Total Number of bytes to be read
            long l_nmTotalBytes = new FileInfo(f_stFilePath).Length;
            //Read Bytes Using BinaryReader Instance
            l_arData = l_BinaryReader.ReadBytes((int)l_nmTotalBytes);
            //Now we have data in DataArray so, just need to close open streams
            l_FileStream.Close();
            l_BinaryReader.Close();
            //return Data array to the calling function
            return l_arData;
        }
Apple iPod touch 32 GB, Case, and Charger Bundle

Tuesday, June 15, 2010

Convert char Array To String

//Take one sample string data
String l_stData = "Hello how are you";

//convert into character array
char[] l_arData = l_stData.ToCharArray();

//Lets convert back to string
l_stData =  new String(l_arData);

Sunday, March 22, 2009

Sunday, March 8, 2009

Monday, January 5, 2009

Friday, December 12, 2008