title: How to read a text file continuously in Windows Service
body: <p>I am reading a text file in windows Service .As per my requirement i have to read the text file line by line and process the contents of the line and insert into the database.The nature of the text file is such that it is constantly getting updated.Sometimes in a span of minute 100 lines will get added and sometimes no.So there is no fixed rate at which lines are getting inserted into the text file.
  At present i am reading the text file using <code>StreamReader</code> line by line with while loop but as soon as line gets over it comes back.Here is my structure of windows Service.</p>
  
  <pre><code>public partial class Service1 : ServiceBase
  {
      public Service1()
      {
          InitializeComponent();
      }
  
      private System.Threading.Thread _thread;
      private ManualResetEvent _shutdownEvent = new ManualResetEvent(false);
      public void OnDebug()
      {
          OnStart(null);
      }
  
      protected override void OnStart(string[] args)
      {
          _thread = new Thread(parseAndProcess);
          _thread.Start();
      }
      public void parseAndProcess()
      {      
          if (System.IO.File.Exists(FileToCopy) == true)
          {
  
              using (StreamReader reader = new StreamReader(FileToCopy))
              {
  
                  string line;
                  while ((line = reader.ReadLine()) != null)
                  {
                      if (line != "")
                      {
                         //line processing logic goes here            
                      }
                      reader.Close();
                  }
              } 
         }
      }
  
      protected override void OnStop()
      {
           _shutdownEvent.Set();
          _thread.Join();  // wait for thread to stop
      }
  }
  }
  </code></pre>
  
  <p>This is the entire structure of my windows Service.Here reading stops if there is no next line in the text file available for that time and it will be stopped until the service is restarted which i dont want to do.
  So how can i check for file updation in case streamreader gets stopped reading lines.</p>
  
  <p>Here is my updated code with FileSystemWatcher </p>
  
  <pre><code>protected override void OnStart(string[] args)
      {
  
          _thread = new Thread(parseAndProcess);
          _thread.Start();
  
  
          FileSystemWatcher Watcher = new FileSystemWatcher("File Path");
          Watcher.EnableRaisingEvents = true;
          Watcher.Changed += new FileSystemEventHandler(Watcher_Changed);
  
  
  
      }
  
      // This event is raised when a file is changed
      private void Watcher_Changed(object sender, FileSystemEventArgs e)
      {
          _thread = new Thread(parseAndProcess);
          _thread.Start();
      }
  </code></pre>
  
  <p>In this Code i have a doubt will my how to read only new lines added into the textfile when getting invoked by FileSystemWatcher Changed Event.
  Please help.</p>