title: How Read a Text File and Display it onto a TextBlock in Visual Studio (C#)
body: <p>I am a newbie in Visual Studio (C#). I want to store a text read from a text file and display it on a TextBlock control, but just for a specified row. How can I do that?
  I've try to search on the internet, and most of them just show the way to read and write.</p>
  
  <p>I have one TextBlock (named 'FlashText'), and two Button (one for the 'Previous' button, another one is for the 'Next' button). What I want is, when I hit the 'Next' button, then the TextBlock showing a text read from a txt file on a specified row (for instance, the first row). And when I hit the 'Next' again, then the TextBlock should be show the second row text read from the file.</p>
  
  <p>The purpose is to make a simple flash card. The code is here:</p>
  
  <p>`</p>
  
  <pre><code>private void btnRight_Click(object sender, RoutedEventArgs e) { 
    string filePath = @"D:\My Workspaces\Windows Phone 7 Solution\SimpleFlashCard\EnglishFlashCard.txt"; 
    int counter = 0;
    string line; 
    System.IO.StreamReader file = new System.IO.StreamReader(filePath); 
    while((line = file.ReadLine()) != null) { 
      Console.WriteLine(line); 
      counter++; 
    } 
  } 
  
  file.Close(); 
  FlashText.Text = Console.ReadLine();
  </code></pre>
  
  <p>`</p>
  
  <p>Please help. Thanks a bunch.</p>
  
  <hr>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>Recently the main code is:</p>
  
  <pre><code>public partial class MainPage : PhoneApplicationPage
  {
      private FlashCard _flashCard;
  
      // Constructor
      public MainPage()
      {
          InitializeComponent();
  
          // This could go under somewhere like a load new flash card button or
          // menu option etc.
          try
          {
              _flashCard = new FlashCard(@"D:\My Workspaces\Windows Phone 7 Solution\FCard\MyDocuments\EnglishFlashCard.txt");
          }
          catch (Exception ex)
          {
              MessageBox.Show(ex.Message);
          }
      }
  
      private void btnLeft_Click(object sender, RoutedEventArgs e)
      {
          DisplayPrevious();
      }
  
      private void btnRight_Click(object sender, RoutedEventArgs e)
      {
          DisplayNext();
      }
  
      private void DisplayNext()
      {
          try
          {
              FlashText.Text = _flashCard.GetNextLine();
          }
          catch (Exception ex)
          {
              MessageBox.Show(ex.Message);
          }
      }
  
      private void DisplayPrevious()
      {
          try
          {
              FlashText.Text = _flashCard.GetPreviousLine();
          }
          catch (Exception ex)
          {
              MessageBox.Show(ex.Message);
          }
      }
  }
  </code></pre>
  
  <p>And this is for the class '<strong>FlashCard</strong>':</p>
  
  <pre><code>public class FlashCard
  {
      private readonly string _file;
      private readonly List&lt;string&gt; _lines;
  
      private int _currentLine;
  
      public FlashCard(string file)
      {
          _file = file;
          _currentLine = -1;
  
          // Ensure the list is initialized
          _lines = new List&lt;string&gt;();
  
          try
          {
              LoadCard();
          }
          catch (Exception ex)
          {
              MessageBox.Show(ex.Message); // This line got a message while running the solution
          }
      }
  
      private void LoadCard()
      {
          if (!File.Exists(_file))
          {
              // Throw a file not found exception
          }
  
          using (var reader = File.OpenText(_file))
          {
              string line;
              while ((line = reader.ReadLine()) != null)
              {
                  _lines.Add(line);
              }
          }
      }
  
      public string GetPreviousLine()
      {
          // Make sure we're not at the first line already
          if (_currentLine &gt; 0)
          {
              _currentLine--;
          }
  
          return _lines[_currentLine]; //-- This line got an error
      }
  
      public string GetNextLine()
      {
          // Make sure we're not at the last line already
          if (_currentLine &lt; _lines.Count - 1)
          {
              _currentLine++;
          }
  
          return _lines[_currentLine]; //-- This line got an error
      }
  }
  </code></pre>
  
  <p>I've got at error message while running the solution: <strong>Attempt to access the method failed: System.IO.File.Exists(System.String)</strong>.</p>
  
  <p>I've tried using breakpoint and while it's getting the LoadCard() method, it's directly thrown to the exception on the constructor. I've rechecked the txt path but it's true.</p>
  
  <p>And I've also got an error message while hitting the 'Next' / 'Previous' button on '<strong>return _lines[_currentLine];</strong>' line said: <strong>ArgumentOutOfRangeException was unhandled</strong> (It's occured on the GetPreviousLine() method if hitting the 'Previous' button and GetNextLine() method for the 'Next'.</p>
  
  <p>Should you need more information I'm glad to provide it. :)</p>
  
  <hr>
  
  <p><strong>UPDATE 2</strong></p>
  
  <p>Here is the recent code:</p>
  
  <pre><code>public partial class MainPage : PhoneApplicationPage
  {
      private string path = @"D:\My Workspaces\Windows Phone 7 Solution\FCard\EnglishFlashCard.txt";
      private List&lt;string&gt; _lines; //-- The error goes here
      private int _currentLineIndex;
  
      //private FlashCard _flashCard;
  
      // Constructor
      public MainPage()
      {
          InitializeComponent();
          //_lines = System.IO.File.ReadLines(path).ToList();
  
          if (File.Exists(path))
          {
              using (StreamReader sr = new StreamReader(path))
              {
                  string line;
                  while ((line = sr.ReadLine()) != null)
                      _lines.Add(line);
              }
          }
  
          CurrentLineIndex = 0;
      }
  
      private void btnLeft_Click(object sender, RoutedEventArgs e)
      {
          this.CurrentLineIndex--;
      }
  
      private void btnRight_Click(object sender, RoutedEventArgs e)
      {
          this.CurrentLineIndex++;
      }
  
      private void UpdateContentLabel()
      {
          this.FlashText.Text = _lines[CurrentLineIndex];
      }
  
      private int CurrentLineIndex
      {
          get {  return _currentLineIndex; }
          set
          {
              if (value &lt; 0 || value &gt;= _lines.Count) return;
              _currentLineIndex = value;
              UpdateContentLabel();
          }
      }
  }
  </code></pre>
  
  <p>I've got the error on the line marked above said: <strong>Field 'FCard.MainPage._lines' is never assigned to, and will always have its default value null</strong>.</p>