Thursday, August 1, 2013

Close a file effectly

I have a piece of code to write a string line to a text file.



foreach (var y in query)
{
TextWriter tw = new StreamWriter(outputFileName, true);
tw.WriteLine(y.Path + "_" + y.Name);
tw.Close();
}



The query has a size of 2000 or more. So in my case I have to open and close file 2000 times. I felt that it is foolish.


No comments:

Post a Comment