简体   繁体   中英

How Do I add data in the notepad .txt file in C#?

I am able to write new data into the .txt file. But, it comes as the replacement of the existing data. So, how can I add new data to the file by keeping the existing data already present in the .txt file. I have been using System.IO namespace and StreamWriter to edit data.

StreamWriter EditEmployeeDetail =
    new StreamWriter(@"C:\Users\user\Desktop\FoodDeliverySystem\Employee_details.txt");
EditEmployeeDetail.Write(temp_Emp_Det);
EditEmployeeDetail.Close();

Use the static File class instead

string path = @"C:\Users\user\Desktop\FoodDeliverySystem\Employee_details.txt";
File.AppendAllText(path, temp_Emp_Det);

AppendAllText opens, writes and closes the file all with one call.

您需要告诉 StreamWriter 您要附加:

var sw = new StreamWriter(path, true);

您需要使用Append选项集打开文件的流。

StreamWriter EditEmployeeDetail = new StreamWriter(@"C:\Users\user\Desktop\FoodDeliverySyste\Employee_details.txt", true);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM