How can I clear the content of a text file using C# ?
Another short version:
System.IO.File.WriteAllBytes(path, new byte[0]);
You can use always stream writer.It will erase old data and append new one each time.
using (StreamWriter sw = new StreamWriter(filePath))
{
getNumberOfControls(frm1,sw);
}
using (FileStream fs = File.Create(path))
{
}
Will create or overwrite a file.
File.WriteAllText(path, String.Empty);
Alternatively,
File.Create(path).Close();
Just open the file with the FileMode.Truncate flag, then close it:
using (var fs = new FileStream(@"C:\path\to\file", FileMode.Truncate))
{
}