使用TextWriter c#清除文本文件的内容

Mic*_*l A 2 c#

如何使用TextWriter对象清除文本文件的所有数据?

mt文件名是:user_name.txt

谢谢.

jb.*_*jb. 9

FileMode.Truncate 将删除文件中的所有文本,然后打开文件进行写入.

FileInfo fi = new FileInfo(@"C:\path\myfile.txt");
using(TextWriter tw = new StreamWriter(fi.Open(FileMode.Truncate)))
{
    tw.Write("write my new text here");
}
Run Code Online (Sandbox Code Playgroud)