How can I clear the content of a text file using C# ?
+12
A:
File.WriteAllText(path, String.Empty);
Alternatively,
File.Create(path).Close();
SLaks
2010-04-23 00:33:59
Wow, even shorter than mine! +1
Dean Harding
2010-04-23 00:35:14
+2
A:
Just open the file with the FileMode.Truncate flag, then close it:
using (var fs = new FileStream(@"C:\path\to\file", FileMode.Truncate))
{
}
Dean Harding
2010-04-23 00:34:56
A:
using (FileStream fs = File.Create(path))
{
}
Will create or overwrite a file.
womp
2010-04-23 00:35:24