c# - How do I keep a text file under maximum size limit? -
this question has answer here:
- good ways limit text log file 4 answers
each time main program runs, writes around 6 lines (around 50 characters in total) log file.
how can keep file growing endlessly? i'd keep file under 2mb new text being added end of file , text @ beginning of file being removed.
fileinfo txtfile = new fileinfo(nameoffile); if (txtfile.length > (2 * 1024 * 1024)) // ## note: 2mb max file size { var lines = file.readalllines(nameoffile).skip(10).toarray(); // ## set 10 lines file.writealllines(nameoffile, lines); }
the above piece of code run if file on 2mb (size, not size on disk). remove first 10 lines file , write remaining lines file.
i have managed work how need small piece of code , thought share here reduce people asking same question. people same issue.
i have not found issues process solution yet, please feel free add comments if incorrect.
Comments
Post a Comment