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
  • 10,905
  • 5
  • 18
  • 32
George Brooks
  • 199
  • 16

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
  • 212,447
  • 27
  • 392
  • 421
1

Try File.WriteAllText, like this:

File.WriteAllText(@"tasks\task1.txt", "");
Ron Beyer
  • 10,758
  • 1
  • 18
  • 35