If you have no desire to keep the contents, or move them into a subsidiary file that tracks the update for the cycle (whether by day or some other cycle length), I would recommend just rewriting the file using this simple method:
private void Truncate(readFile) // to clear contents of file and note last time it was cleared
{
string readFile = readPath + ".txt";
string str = string.Format("{0} : Truncated Contents", DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss tt"));
using (StreamWriter truncate = new StreamWriter(readFile))
{
truncate.WriteLine(str); // truncates and leaves the message with DateTime stamp
}
}
If, on the other hand, you want to save the contents to a file for the date they were truncated, you can use the following method in conjunction with the above:
private void Truncate(readPath) // to clear contents of file, copy, and note last time it was cleared and copied
{
if (!File.Exists(readPath)) // create the new file for storing old entries
{
string readFile = readPath + ".txt";
string writeFile = readPath + DateTime.Now.ToString("_dd-MM-yyyy_hh-mm") + ".txt"; // you can add all the way down to milliseconds if your system runs fast enough
using (FileStream fs = new FileStream(writeFile, FileMode.OpenOrCreate, FileAccess.Write))
{
using (StreamWriter write = new StreamWriter(fs))
using (StreamReader file = new StreamReader(readFile))
{
write.WriteLine(string.Format(textA, DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss tt")));
string line;
var sb = new StringBuilder();
while ((line = file.ReadLine()) != null)
{
line = line.Replace("\0", ""); // removes nonsense bits from stream
sb.AppendLine(line);
}
write.WriteLine(sb.ToString());
string textB = "{0} : Copied Source";
write.WriteLine(string.Format(textB, DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss tt")));
}
}
string str = string.Format("{0} : Truncated Contents", DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss tt"));
using (StreamWriter truncate = new StreamWriter(readFile))
{
truncate.WriteLine(str); // truncates and leaves the message with DateTime stamp
}
}
}
Either way, you can utilize the method of your choice with the following block:
if(new FileInfo("audit.txt").Length >= 0xfffff) // hex for 1MB
{
Truncate("audit");
}
I hope this helps future readers.
Thanks,
C§