将文本附加到txt文件c#

时间:2014-01-13 15:44:45

标签: c#

所以我有一个名为DataCourses.txt的txt文件 每5行代表1门课程的信息。我已经有20行(4门课程),我试图使用StreamWriter在那里添加信息。我试过:

using (StreamWriter sw = new StreamWriter("DataCourse.txt", true);
{
    sw.WriteLine(tabCourses[Current].Number = txtNumber.Text);
    sw.WriteLine(tabCourses[Current].Title = txtTitle.Text);
    sw.WriteLine(tabCourses[Current].Duration = Convert.ToInt16(txtDuration.Text));
    sw.WriteLine(tabCourses[Current].Weight = txtWeigthing.Text);
    sw.WriteLine(tabCourses[Current].Language = cmbLanguage.SelectedItem.ToString());
}

但是它没有工作......我尝试使用infront,但是当我尝试输入sw.WriteLine时,我得到一个错误。我做错了什么?

编辑:问题是我错过了括号和半列;

2 个答案:

答案 0 :(得分:4)

您的using语句在该行的末尾结束,并且缺少结束的parenthesys

using (StreamWriter sw = new StreamWriter("DataCourse.txt", true);
                                                                ^^^

应该是

using (StreamWriter sw = new StreamWriter("DataCourse.txt", true))
{
   .....
}

答案 1 :(得分:-1)

File.AppendAllText("DataCourse.txt", "Your String");