tags:

views:

95

answers:

3

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
Wow, even shorter than mine! +1
Dean Harding
+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
Do you mean `FileStream`?
SLaks
@SLaks: oops, thanks!
Dean Harding
A: 
 using (FileStream fs = File.Create(path))
 {

 }

Will create or overwrite a file.

womp
Since there's no code in the block, the `using` statement offers no advantage over `.Close()`.
SLaks
I'm assuming he'd be doing something with the file.
womp