簡體   English   中英

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

[英]clear content of a text file using TextWriter c#

如何使用TextWriter對象清除文本文件的所有數據?

mt文件名是:user_name.txt

謝謝。

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");
}

File.Create(filename).Close();
也為你工作?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM