Can you use XML?  If so, you can easily handle this using an XML log file.

-----Original Message-----
From: Discussion of advanced .NET topics.
[mailto:[EMAIL PROTECTED] On Behalf Of Girish Jain
Sent: Friday, November 18, 2005 6:22 AM
To: [email protected]
Subject: [ADVANCED-DOTNET] Writing data to the beginning of a file

Hi All,

I am writing a log file with the requirement that the latest entry in the
log should be on the top. For the purpose I looked for ways of doing that
but ended up by reading the entire contents of the existing file into a byte
array and then re-write the file with the new content. Is there any better
way possible?

The current code

// Firstly read the existing file and take the contents // into a byte array
//------------------------------------------------------------
fs = new FileStream(m_strFilePath, FileMode.Open, FileAccess.Read); byte[]
arrOriginalText = new byte[fs.Length]; fs.Read(arrOriginalText, 0,
arrOriginalText.Length); fs.Close(); fs = null;
//------------------------------------------------------------


// Now write the data to the file by re-creating the new file
//------------------------------------------------------------
fs = new FileStream(m_strFilePath, FileMode.Create, FileAccess.Write);


// Convert the new data to be written to a byte array
//------------------------------------------------------------
byte[] arrNewText = new byte[ strData.Length ]; Encoding.UTF8.GetBytes(
strData, 0, strData.Length, arrNewText, 0 );


// Now write data - first the new text and then the original
//------------------------------------------------------------
fs.Write(arrNewText, 0, arrNewText.Length); fs.Write(arrOriginalText, 0,
arrOriginalText.Length);

//------------------------------------------------------------


Any suggestion would be highly appreciated

Thanks in advance

Regards,
Girish Jain

===================================
This list is hosted by DevelopMentorR  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

===================================
This list is hosted by DevelopMentorĀ®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to