This should be an extremely easy fix, but for some reason I am missing something. All I am trying to do is get the String Builder function that I have to write the header, but for some reason it isnt currently.
When I try to change the if statement to !File.Exists(tempFileName), it does not run through my loop.
Any suggestions? Also, let me know if you need more info. Thanks in advance.
public static void Open(string tempFileName, string division,
int zipFiles, int conversions, int returnedFiles, int totalEmails)
{
StreamWriter dailyStats;
//This is where I am missing something
//I am passing in the original filename of a log, then adding "-Stats.log"
//so I can tell the difference between what is the new stats file, and the original log file
if (File.Exists(tempFileName))
{
dailyStats = new StreamWriter(tempFileName + "-Stats.log");
StringBuilder sb = new StringBuilder();
sb.Append("Division");
sb.Append("\t");
sb.Append("Zip Files");
sb.Append("\t");
sb.Append("Conversions");
sb.Append("\t");
sb.Append("Returned Files");
sb.Append("\t");
sb.Append("Total E-Mails");
sb.Append("\t");
}
else
{
dailyStats = File.AppendText(tempFileName + "-Stats.log");
}
if (writeLog)
{
//Use a string builder to assemble the content for performance reasons
StringBuilder s = new StringBuilder();
s.Append(division);
s.Append("\t");
s.Append(zipFiles);
s.Append("\t");
s.Append(conversions);
s.Append("\t");
s.Append(returnedFiles);
s.Append("\t");
s.Append(totalEmails);
s.Append("\t");
dailyStats.WriteLine(s.ToString());
}
dailyStats.Close();
}
You can fix it like this
UPDATE
The code had different errors. Two
StringBuilderswere created, but only one was written to the file. The existence of the file was determined for a different file name than the actual file that was written to. And finally, the logic depending on the existence of the file was inverted. I rewrote and refactored the code completely, in order to make it more understandable and manageableNote: The using statement closes the file and releases the external resources automatically.