Home > Software engineering >  Is there a way to delete all lines of text from a .txt file using StreamReader/Writer?
Is there a way to delete all lines of text from a .txt file using StreamReader/Writer?

Time:09-04

I am using C# with Unity. I have a text file in my assets that is updated by a void function via StreamWriter. I want to remove all lines of text from the file (making it blank) upon the execution of another function. Is there a way to do this? I've had difficulty finding an appropriate method in the documentation.

CodePudding user response:

When you call your streamwriter contructor, set the parameter append to false When you open the txt file, you will see that only the next text is available.

string fileName = @"test.txt";
StreamWriter writer;
using (writer = new StreamWriter(fileName, false)) { 
    writer.Write("new text");
}
  • Related