C# Streamwriter覆盖线

C# Streamwriter覆盖线,c#,winforms,overriding,streamwriter,logfile,C#,Winforms,Overriding,Streamwriter,Logfile,程序:显示图像,对于该图像,用户可以选择不同的单选按钮选项,当用户提交时,图像路径和单选按钮的值写入日志文件 问题:streamwriter会覆盖每一行,因此只有最后一行会写入日志文件 代码流编写器: private void writeLogFile(string textInLog) { //Creating a streamwriter to write to the file with the path of logFileName. using (var sw = ne

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

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

代码流编写器:

private void writeLogFile(string textInLog)
{
    //Creating a streamwriter to write to the file with the path of logFileName.
    using (var sw = new StreamWriter(logFileName))
    {
        sw.WriteLine(textInLog);
    }
}
我告诉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);
};
submitButton.Click+=(发件人,参数)=>
{
路径字符串=”;
totalStringForRadioButtons=“”;
totalStringForEachPath=“”;
var panels=Controls.OfType().ToList();
foreach(面板中的面板p)
{
var selectedRadioButton=p.Controls.OfType().FirstOrDefault(rb=>rb.Checked);
如果(selectedRadioButton!=null)
{
totalStringForRadioButtons+=$“{selectedRadioButton.Name}:{selectedRadioButton.Text}|”;
}
}
pathString=allFiles[index].ToString();
totalStringForEachPath+=pathString+“:“+totalStringForRadioButtons+”\r\n”;
//将数据写入日志文件
writeLogFile(totalStringForEachPath);
changeImage(假,真,假,假,假,假,假);
};

是重复的,很抱歉用以下方法修复了它:

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);
    }
}