0

I want to be able to click a picture box that will clear a text files text not delete the text file but just remove the text inside sort of like resetting it. I want the picture box to work as a delete button pretty much. I can save text into the file and read it I just want to clear its contents.

StreamReader script = new StreamReader(@"tasks\task1.txt"); //locates task1
        script == null;

That is sort of what I have but this does not work

braX
  • 11,506
  • 5
  • 20
  • 33
George Brooks
  • 199
  • 2
  • 17

2 Answers2

3

Simply Use File.WriteAllText:

File.WriteAllText("yourFile.txt", "");

This will erase all the text in the text file and replace it with empty string and you will end up with a 0 byte file.

File.WriteAllText Method (String, String)

Creates a new file, writes the specified string to the file, and then closes the file. If the target file already exists, it is overwritten.

Habib
  • 219,104
  • 29
  • 407
  • 436
1

Try File.WriteAllText, like this:

File.WriteAllText(@"tasks\task1.txt", "");
Ron Beyer
  • 11,003
  • 1
  • 19
  • 37