Vjatseslav Krosin

Vjatseslav Krosin

  • NA
  • 34
  • 2.1k

Problem with using 2 StreamReaders

Aug 31 2011 11:58 AM
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 != nullif (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?

Answers (2)