看板 C_Sharp 關於我們 聯絡資訊
string write="test.txt"; StreamWriter WriteFile = new StreamWriter(write, true); //開啟"test.txt" WriteFile.WriteLine("XD"); WriteFile.Flush(); //清除緩衝區 WriteFile.Close(); //關閉檔案 上面那個true,就是添加在檔案後面, 若是false,則是覆蓋檔案. 為了避免I/O出問題 務必記得清除緩衝區, (Flush) 還有記得要把檔案關閉. (Close) 若有需要可搭配先讀檔再寫檔的方法, 視需求而定 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 118.169.233.116
F23ko:原來如此 05/05 18:41
aecho:咦?可以請教,沒Flush()的話會有什麼問題嗎? 05/06 06:38
liaommx:我自己沒有測試過,不過依照其他人的說法, 05/06 07:09
liaommx:再調用同一個名稱的writeline可能會I/O錯吧 05/06 07:10
liaommx:所以我就習慣了,每次write都flush 05/06 07:10