Guest Deleted member 22320 Posted May 21, 2003 Posted May 21, 2003 For instance, say you got a program that keeps a log of the activities it performs. Does anyone have any idea on how to set a max size you would want that file to be? like say 1mb. I was thinking maybe there is some way to load the pre-existing contents into an array of somekind and seeing how big it is, and then adding to the array throughout the runtime of the program then write it out when finished.. That way you could even reverse the order of the log, putting the newest entries at the top for easy viewing. the only problem with that is I want to flush the buffer and write to the file after every logged action. That way if something goes wrong and crashes at least there is something in the log file. Any ideas? Quote
aewarnick Posted May 30, 2003 Posted May 30, 2003 You could count the characters. I just did a test with a text file and found that one character is one byte and a new line is 2 bytes. Quote C#
*Gurus* divil Posted May 30, 2003 *Gurus* Posted May 30, 2003 If your text file is in unicode, there will be two bytes per character. The easiest method of checking a file's size is to use the FileInfo class and check its Length property. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
aewarnick Posted May 30, 2003 Posted May 30, 2003 Does FileInfo's Length property return the size in bytes? Quote C#
Guest Deleted member 22320 Posted June 1, 2003 Posted June 1, 2003 Yes it does, actually I found a nice little solution to this, heres the code: 'limit the files to 250K Dim TR As System.IO.TextReader Dim sTemp As String = "" Dim iTot As Integer = 0 Dim sTempRemoved As String = "" TR = New System.IO.StreamReader(m_AppName) sTemp = TR.ReadToEnd() TR.Close() iTot = sTemp.Length - 250000 If iTot < 0 Then iTot = 0 End If sTempRemoved = sTemp.Remove(0, iTot) TWR = New System.IO.StreamWriter(m_AppName, False) TWR.Write(sTempRemoved) TWR.Close() or in c# how I originally got it //limit the files to 250K System.IO.TextReader TR; string sTemp = ""; int iTot = 0; string sTempRemoved = ""; TR = new System.IO.StreamReader(m_AppName); sTemp = TR.ReadToEnd(); TR.Close(); iTot = sTemp.Length - 250000; if (iTot < 0){iTot = 0;} sTempRemoved = sTemp.Remove(0, iTot); TWR = new System.IO.StreamWriter(m_AppName,false); TWR.Write(sTempRemoved); TWR.Close(); Quote
aewarnick Posted June 1, 2003 Posted June 1, 2003 You didn't use FileInfo though. You used a stream. But it works. Quote C#
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.