-1

Program: An image gets shown, for this image the user can select different radiobuttons options, when user submits, image path and the values of radiobuttons gets written in the logfile.

Problem: The streamwriter overrides every line, so only the last line will be written in the logfile.

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

Code where i tell streamwriter to write:

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);
};
Niels
  • 95
  • 2
  • 9

1 Answers1

0

Was duplicate, sorry fixed it with:

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);
    }
}
Niels
  • 95
  • 2
  • 9