Hi, i have a problem using 2 StreamReaders like this:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
FileInfo file = new FileInfo("key.txt");
FileInfo file2 = new FileInfo("value.txt");
StreamReader stRead = file.OpenText();
using (stRead)
{
while (!stRead.EndOfStream)
{
string key = stRead.ReadLine();
listBox1.Items.Add(key);
countItems++;
numberCount.Content = countItems.ToString();
}
}
StreamReader stRead2 = file2.OpenText();
using (stRead2)
{
while (!stRead2.EndOfStream)
{
string key = stRead.ReadLine();
string value = stRead2.ReadLine();
if (value != null) if (key != null) list.Add(key, value);
}
}
}
The first one works fine (stRead), but when it gets to second one (stRead2), then it get the while statement !stRead2.EndOfStream == true and it skips the loop for some reason, can anyone help me? And will the list.Add() even work in the list?
Loading
Mayur DighePosted Aug 31, 2011, 12:31 PM
Hi Vjatseslav Krosin,
you are doing only one mistake that,
in second loop you try to read file from "stRead2" but you also added the "stRead" in loop so that you get error.
StreamReader does not allow to read file unless you open it properly
plz do change as neccessary ....
you can do it by taking "key" value for stRead2 from ListBox that you use.
using (stRead2)
{
while (!stRead2.EndOfStream)
{
string key = stRead.ReadLine();
string value = stRead2.ReadLine();
if (value != null) if (key != null) list.Add(key, value);
}
}
Vjatseslav KrosinPosted Aug 31, 2011, 12:41 PM