Jump to content
Xtreme .Net Talk

Anyone know how to set a maximum text file size?


Recommended Posts

Guest Deleted member 22320
Posted

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?

  • 2 weeks later...
Guest Deleted member 22320
Posted

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(); 

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...