• 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.

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

I'm looking for sample code.
0
 
karthika_ctsCommented:
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


DELETE COMMAND:
============
MyFile.Delete()
0
 
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.
0
 
andr_ginCommented:
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.
0
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