I want to clean some volume of my text log file if it size more then max:
FileInfo f = new FileInfo(filename);
if (f.Length > 30*1024*1024)
{
var lines = File.ReadLines(filename).Skip(10000);
File.WriteAllLines(filename, lines);
}
But I have exception
System.IO.IOException: The process cannot access the file '<path>' because it is being used by another process.
Questions:
- Do I need close FileInfo object before future work with file?
- Is there some more adequate method to rotate logs? (for example eficciant way to obtain number of lines instead of it byte size? )