How can I clear the content of a text file using C# ?
If you simply open the file for writing with the truncate-option, you'll delete the content.
Open the file with your text editor and press End. Highlight and PgUp to delete the remaining bytes that don't belong (usually recognizable by ASCII garbage characters).
If you switch between input and output on a file opened for update mode, you must use a file positioning operation ( fseek() , rewind() , nominally fsetpos() ) between reading and writing; and you must use a positioning operation or fflush() between writing and reading.
File.WriteAllText(path, String.Empty);
Alternatively,
File.Create(path).Close();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With