Post

Make a file empty

In General on April 26, 2008 by Sid

Found someone using Google to figure out how to clear out the contents of a file and hitting my blog. Well, it’s pretty easy – use FileMode.Truncate when opening the file.

using (FileStream fs = File.Open(filepath, FileMode.Truncate, FileAccess.Write))
{
// Code to write to the file goes here
fs.Close();
}

Leave a Reply