I have a text file to which I will be appending data(hence I can't just overwrite the file). The thing is that originally it does contain content I do not want, so before I start appending new data. Is there a way to clear the file without having to delete and then re-create it?
-
Rename it and then re-create it? – Martin James Dec 23 '14 at 14:21
-
http://stackoverflow.com/questions/8464261/filestream-and-streamwriter-how-to-truncate-the-remainder-of-the-file-after-wr ? – Marc B Dec 23 '14 at 14:21
-
Why not just overwrite the file to start with -- with an empty string -- and then append your data afterwards? – rory.ap Dec 23 '14 at 14:22
-
1Overwriting it with an empty string sounds good. Is File.WriteAllText the best option? – Coding Duchess Dec 23 '14 at 14:27
-
1Yes, you could use that. – rory.ap Dec 23 '14 at 14:39
-
Opening the file and replacing the contents before adding more text, is just the long way of overwriting. – Ňɏssa Pøngjǣrdenlarp Dec 23 '14 at 14:43
-
@Plutonix -- Not if you want to preserve the original create date and other metadata. – rory.ap Dec 23 '14 at 14:51
-
roryap, could you post a solution so I could accept it - I went with your suggestion: File.WriteAllText(Path,"") – Coding Duchess Dec 23 '14 at 16:42
-
What function are you going to write all the new data with? Both `My.Computer.FileSystem.WriteAllText()` and `System.IO.File.WriteAllLines()` will overwrite **and** truncate the file if there was any old data in it. I don't see why you need to explicitly make it blank beforehand... – Idle_Mind Dec 23 '14 at 17:46
-
Idle_mind, that's because then I will append to the file inside the loop. So if I use WriteAllText then, it will clear the file every iteration – Coding Duchess Dec 24 '14 at 16:28
3 Answers
You can start by overwriting the file with an empty string, and then append your data afterwards. You could use this to overwrite the file:
System.IO.File.WriteAllText(Path, "")
The benefit of this as opposed to deleting and recreating the file is that you will preserve the original create date of the file along with any other metadata.

- 34,009
- 10
- 83
- 174
Yes, most .NET methods allow you to either append or overwrite. You already found one method that could do it - File.WriteAllText. The only drawback is that it's non-streamed, so for a lot of content, you may need a lot of memory. File.Create is a stream version. It may look like more code (and it is), but it can be useful in situations when you simply cannot afford to put all your contents in memory at once. Example from MSDN:
Dim path As String = "c:\temp\MyTest.txt"
' Create or overwrite the file.
Dim fs As FileStream = File.Create(path)
' Add text to the file.
Dim info As Byte() = New UTF8Encoding(True).GetBytes(
"This is some text in the file.")
fs.Write(info, 0, info.Length)
fs.Close()

- 25,801
- 18
- 85
- 151
http://msdn.microsoft.com/en-us/library/system.io.filestream.setlength(v=vs.110).aspx
FileStream.SetLength(0);
Set the FileStream Length to zero. I have included the docs from MSDN.

- 1,132
- 1
- 7
- 13