• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 406
  • Last Modified:

How to limit size of data file

I have a log file that I am adding error messages to as my application runs along, e.g. logfile.AppendText. I would like help on how I can limit the size of the file and possible delete the first stored line, when the file has reached it's max limit, so the new AppendText will add the error to end of the file, so a FiFo file type system in Visual Basic.

2 Solutions
käµfm³d 👽Commented:
how I can limit the size of the file
By file size, or number of lines?
atomicgs12Author Commented:
kaufmed: -
Your joking correct?

I'm looking for sample code.
Hope it might help

To Get File Size:

Imports System.IO

Private Function GetFileSize(ByVal MyFilePath As String) As Long
Dim MyFile As New FileInfo(MyFilePath)
Dim FileSize As Long = MyFile.Length
Return FileSize
End Function

käµfm³d 👽Commented:
Your joking correct?

Hmmm...  I don't remember putting a winking emoticon or a "j/k" in my post...  maybe I'm getting senile.
1.) Checking the file size has been answered before.

2.) Deleting the first line of a file is not possible without writing the whole file again, because all content of the file would have to be shifted by xx bytes.
What you can do is reading the file with My.Computer.FileSystem.ReadAllText, cut off the first x lines (for example the first 10% or the half of the file) and write the changed file back with My.Computer.Filesystem.WriteAllText
A more common way is to do log rotation. You have 2 log files. If one is full, you delete the other file and write into a new one.
Another way I have used is simple reducing the amount of logged data. If the file is empty I log every event, if the file is full I log only critical errors.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now