title: Searching for a word in a text file in C#
body: <p>I have a small txt file containing three lines of text. I'm trying to create a programme in C# that gets a word from a user and searches for that word in the txt file. If the word is found, I'm wanting to record and display what lines of the txt file the word was found on.  The variable int position records if the word is found or not, but I can't work out how to record what lines the word is found on. How would I do this? Here is my code:</p>
  
  <pre><code>class Program {
      static void Main(string[] args) {
          Console.Write("please enter a file to search for");
          string fileResponse = Console.ReadLine();
          Console.Write("please enter a word to search for in the file");
          string wordResponse = Console.ReadLine();
          StreamReader myfile = File.OpenText(fileResponse);
          string line = myfile.ReadLine();
          int position = line.IndexOf(wordResponse);
          int count = 0; //counts the number of times wordResponse is found.
          int lineNunmber = 0;
  
              while (line != null) {
                  if (position != -1) {
                      count++;
                  }
                  line = myfile.ReadLine();
              }
           if (count == 0) {
              Console.WriteLine("your word was not found!");
           } else {
              Console.WriteLine("Your word was found " + count + " times!" + position);
           }
           Console.ReadLine();
      }
  }
  </code></pre>