I have a program file in C# that in some point looks like this:
var lowest3Students = studentAVGsOrdered.Take(3);
using var writer4 = GetStreamWriter(folder, "file4.txt");
WriteStudents(writer4, lowest3Students, false);
double AVGTotal = allScores.Average();
using var writer5 = GetStreamWriter(folder, "file5.txt");
writer5.WriteLine(AVGTotal.ToString());
void WriteStudents(StreamWriter writer, IEnumerable<StudentAVG> studentAVGs,
bool includeName = true)
{
foreach (var student in studentAVGs)
{
if (includeName)
{
writer.Write(student.Student);
writer.Write(",");
}
writer.Write(student.AVGScore.ToString());
writer.Write(writer.NewLine);
}
}
StreamWriter GetStreamWriter(string folder, string fileName)
{
var file = Path.Combine(folder, fileName);
//using FileStream fileStream = new FileStream(file, FileMode.Create, FileAccess.Write);
StreamWriter streamWriter = new StreamWriter(file, true);
return streamWriter;
}
I have five files to be created and when I debug the write method, it writes the required data, but when I go to bin\debug and inspect created txt files (5 files: file1 to file5), There are No content inside. What should be done for this to work?
After writing to the file I put this line that solved my problem:
writer5.Flush();