Streamwriter覆盖线

时间:2017-10-16 13:03:49

标签: c# winforms override streamwriter logfile

程序:显示图像,对于此图像,用户可以选择不同的单选按钮选项,当用户提交时,图像路径和无线电按钮的值将写入日志文件。

问题:streamwriter会覆盖每一行,因此只会在日志文件中写入最后一行。

代码编写器:

submitButton.Click += (sender, args) =>
{
    pathString = "";
    totalStringForRadioButtons = "";
    totalStringForEachPath = "";
    var panels = Controls.OfType<Panel>().ToList();
    foreach (Panel p in panels)
    {
         var selectedRadioButton = p.Controls.OfType<RadioButton>().FirstOrDefault(rb => rb.Checked);
         if (selectedRadioButton != null)
         {
              totalStringForRadioButtons += $"{selectedRadioButton.Name} : {selectedRadioButton.Text} | ";
         }
    }

    pathString = allFiles[index].ToString();
    totalStringForEachPath += pathString + " : " + totalStringForRadioButtons + "\r\n";

    //writedata to log file
    writeLogFile(totalStringForEachPath);
    changeImage(false, true, false, false, false, false);
};

我告诉写作者写的代码:

df=pd.DataFrame([[11132,'Jim','Jan',2012,2,'A'],[11132,'Jim','Jan',2012,5,'U'],
[11132,'Jim','Feb',2012,6,'Z'],[11132,'Jim','Feb',2012,4,'A'],
[2755,'Jack','Apr',2012,1,'U'],[2755,'Jack','Apr',2012,3,'A'],[2755,'Jack','Apr',2012,1,'Z'],
[9753,'Sue','Jan',2012,8,'U'],[9753,'Sue','Feb',2012,1,'Z'],[9753,'Sue','Jan',2012,5,'A'],],columns=['ID_Patient', 'Name_Patient','month','year','var','visits'])

df
Out[744]: 
   ID_Patient Name_Patient month  year  var visits
0       11132          Jim   Jan  2012    2      A
1       11132          Jim   Jan  2012    5      U
2       11132          Jim   Feb  2012    6      Z
3       11132          Jim   Feb  2012    4      A
4        2755         Jack   Apr  2012    1      U
5        2755         Jack   Apr  2012    3      A
6        2755         Jack   Apr  2012    1      Z
7        9753          Sue   Jan  2012    8      U
8        9753          Sue   Feb  2012    1      Z
9        9753          Sue   Jan  2012    5      A

1 个答案:

答案 0 :(得分:0)

重复,抱歉用以下方法修复:

private void writeLogFile(string textInLog)
{
    //Creating a streamwriter to write to the file with the path of logFileName.
    using (FileStream fs = new FileStream(logFileName, FileMode.Append, FileAccess.Write))
    using (var sw = new StreamWriter(fs))
    {
         sw.WriteLine(textInLog);
    }
}