I am having some trouble writing a string MentionsInText
to a file using the Systems.IO.StreamWriter
according to the file it is edited when the code is run however, no text is present in the file. I am not sure why this is not working.
My code is as follows;
var MIS = string.Join(" ", MentionsList.ToArray());
string Mentionsintext = MIS.ToString();
StreamWriter MentionFile = new StreamWriter(@"C:\Users\User\Documents\Mentions.txt");
MentionFile.WriteLine(Mentionsintext + Environment.NewLine);
Am I doing something wrong when using StreamWriter
?
You should dispose of the StreamWriter after writing to it.
eg:
var MIS = string.Join(" ", MentionsList.ToArray());
string Mentionsintext = MIS.ToString();
using (StreamWriter MentionFile = new StreamWriter(@"C:\Users\User\Documents\Mentions.txt")) {
MentionFile.WriteLine(Mentionsintext + Environment.NewLine);
}
For more examples: see https://www.dotnetperls.com/streamwriter