如何让 Streamwriter 统一覆盖文本文件 (c#)?
How can I make Stream Writer overwrite a txt file in unity (c#)?
我正在尝试使用文本文件来跟踪一些信息。我希望每次写入时重置文本文件,这样我就不会得到一堆旧信息。我有什么办法可以做到这一点吗?
void WriteString(string text, string path) {
StreamWriter writer = new StreamWriter(path, true);
writer.Write(text);
writer.Close();
}
// Your Text File Path
string path = @"";
// The Text You Want To Write inside The File
string content = "";
// This Method Clears Whatever Text It Finds in the File and Writes the new Content (overwrites the text file)
File.WriteAllText(path, content);
如果在构造函数调用中将“true”更改为“false”,它将覆盖文件。
StreamWriter writer = new StreamWriter(path, false);
我正在尝试使用文本文件来跟踪一些信息。我希望每次写入时重置文本文件,这样我就不会得到一堆旧信息。我有什么办法可以做到这一点吗?
void WriteString(string text, string path) {
StreamWriter writer = new StreamWriter(path, true);
writer.Write(text);
writer.Close();
}
// Your Text File Path
string path = @"";
// The Text You Want To Write inside The File
string content = "";
// This Method Clears Whatever Text It Finds in the File and Writes the new Content (overwrites the text file)
File.WriteAllText(path, content);
如果在构造函数调用中将“true”更改为“false”,它将覆盖文件。
StreamWriter writer = new StreamWriter(path, false);