ACwing 422校門外的樹
阿新 • • 發佈:2021-01-26
技術標籤:c#
c# |指定地址建立檔案,文字的讀寫
參考:https://www.runoob.com/csharp/csharp-file-io.html
1. 在D:\Sample檔案目錄下建立sample.txt文字檔案,並向其中寫入字元資料;若檔案已存在,則覆蓋原本的內容寫入指定字元資料,若檔案不存在,則建立新的檔案寫入字元資料
using System;using System.IO;using System.Text;
string path = @"D:\Sample\sample.txt";//設定指定地址D:\Sample // This text is added only once to the file. if (File.Exists(path)) { string CreateTxt = "Please close the window" + Environment.NewLine;//Environment.NewLine是Environment類的換行的意思 File.WriteAllText(path, CreateTxt); } //如果檔案不存在,則建立新的檔案 else { string AppendText = "Please open the window" + Environment.NewLine; File.AppendAllText(path, AppendText); } //開啟檔案內容讀取到執行視窗 Console.WriteLine(File.ReadAllText(path)); Console.ReadLine();
WriteAllText和AppendAllText的區別