StreamWriter limit in C# in text file

会有一股神秘感。 提交于 2019-11-29 13:56:39

You need to call StreamWriter.Flush or set StreamWriter.AutoFlush to true. That said, if you use using statment, everything should work fine.

using(StreamWriter sw = new StreamWriter(fs))
{
    ArrayList chartList = GetChart(maintNode);    
    foreach (var line in chartList)
    {
        sw.WriteLine(line);
    }
}

Using statement calls Dispose which will flush the buffer to the FileStream and also closes the file stream. So you don't need to close it manually.

Then I recommend List<T> over ArrayList. ArrayList shouldn't be used, it is not type safe and should be avoided if you're in .Net2.0 or greater.

Also consider using File.WriteAllLines method, so that you don't need these many lines of code. Everything is managed by WriteAllLines method itself.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!