How to empty my text document if document size exceeds specified size [Resolved]

Posted by Prabhukiran345 under C# on 1/9/2014 | Points: 10 | Views : 1775 | Status : [Member] | Replies : 2
Hi,

I got a scenario that i am saving my error log in a text document. Now i want to clear my log file if the file size exceeds "2kb" .. Can anyone help me to solve this scenario.

Thanks in Advance

Regards,
Prabhu Kiran B

Thanks,
Prabhu Kiran Bommareddy



Responses

Posted by: kgovindarao523-21772 on: 1/9/2014 [Member] [MVP] Bronze | Points: 50

Up
0
Down

Resolved
Hi,

Try this code:

string[] files = Directory.GetFiles(@"YOUR FILE PATH");
foreach (string file in files)
{
FileInfo fi = new FileInfo(file);
if (fi.Length>2048) //length is in bytes, here 2kb=2*1024
{
using (var stream = new FileStream(file, FileMode.Truncate))
{
using (var writer = new StreamWriter(stream))
{
writer.Write("");
}
}
}
}



Thank you,
Govind

Prabhukiran345, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Prabhukiran345 on: 1/9/2014 [Member] Starter | Points: 25

Up
0
Down
Thanks Govind.. U saved my time..

Thanks,
Prabhu Kiran Bommareddy

Prabhukiran345, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response