I have a lot of text files and want to read them all by once, how do I do this? This is my code till now:
List<StreamReader> lijst = new List<StreamReader>();
using (StreamReader qwe = new StreamReader("C:\\123.txt"))
using (StreamReader qwer = new StreamReader("C:\\1234.txt"))
lijst.Add(qwe);
lijst.Add(qwer);
But I get an ObjectDisposedException(Cannot read from a closed TextReader.) when doing this:
lijst[0].Readline();
Any idea how to fix this? Thansk in advance
You are not using curly braces, so you cannot see where the object is disposed. You code is identical to this code:
This means that when you get to the last line of this code your stream readers are already disposed. In your case you should not use
using
, but you need to make sure to dispose the stream readers afterwards: