title: How to read a text file for a specific word in lines and write the entire lines to another text file?
body: <p>I have a text file (say text1.txt) with the following lines:</p>
  
  <pre><code>4410 Rel testRel1
  4411 Dbg testDbg1
  4412 Dbg testDbg2
  4412 Rel testRel2
  4413 Rel testRel3
  4413 Dbg testDbg3
  4414 Rel testRel4
  4415 Rel testRel5
  </code></pre>
  
  <p>Now, I want to write all the lines with word "Rel" to a text file (say text2.txt). So my text2.txt should look like:</p>
  
  <pre><code>4410 Rel testRel1
  4412 Rel testRel2
  4413 Rel testRel3
  4414 Rel testRel4
  4415 Rel testRel5
  </code></pre>
  
  <p>Atlast, My code should read text2.txt return the first four characters of the last line of text2.txt(i.e. 4415) taking path of text1.txt as input.
  Below is my code. May be I may have written half of it and with no idea of c#.</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Activities;
  using Microsoft.Build.Utilities;
  using Microsoft.Build.Framework;
  using System.IO;
  using Microsoft.TeamFoundation.Build.Client;
  
  namespace Build_Tasks.Activities
  {
      [BuildActivity(HostEnvironmentOption.All)]
      public sealed class GetBuildNumber : CodeActivity
      {
          // Define an activity input argument of type string
          public InArgument&lt;string&gt; TextFileName { get; set; }
          public OutArgument&lt;string&gt; setBuildNumber { get; set; }
          private string temp_FilePath = "c:\\temp.txt";
  
          // If your activity returns a value, derive from CodeActivity&lt;TResult&gt;
          // and return the value from the Execute method.
          protected override void Execute(CodeActivityContext context)
          {
              // Obtain the runtime value of the Text input argument
              string TextFileName = context.GetValue(this.TextFileName);
              string TextFilePath = TextFileName;
              string[] Lines = File.ReadAllLines(TextFilePath);
              //string wordrel = "Rel";
              System.IO.StreamReader file = new System.IO.StreamReader(TextFilePath);
              List&lt;string&gt; Spec = new List&lt;string&gt;();
              string line;
              System.IO.StreamWriter file2 = new System.IO.StreamWriter(temp_FilePath);
              while ((line = file.ReadLine()) != null)
              {
                  if(line.Contains("Rel"))
                       {
                          file2.WriteLine(line);
                        }
              }
              var lastline = File.ReadLines(temp_FilePath).Last();
              string number = lastline.Substring(0, 4);
              context.SetValue&lt;string&gt;(this.setBuildNumber, number);
              }
      }
      }
  </code></pre>