title: C# File Monitoring
body: <p>This applies to a previous question I had asked.  I have been trying to create an application that will allow me to monitor a file and alert me if the file has stopped changing.  Someone recommended just checking the file size and using a timer in a Windows service.  I have taken their advice but I am not very good with Windows Services.  I am trying to do the file comparison between two files that I have created.  Below is the code</p>
  
  <pre><code>public partial class Service1 : ServiceBase
  {
      Timer timer1 = new Timer();
      string filePath = @"C:\Users\Test\Desktop\New folder\Test.txt";
      string logPath = @"C:\Users\Test\Desktop\New folder\Log.txt";
      string newLog = @"C:\Users\Test\Desktop\New folder\Log1.txt";
  
  
      public Service1()
      {
          InitializeComponent();
      }
  
      public void OnStart(string[] args)
      {            
          timer1.Elapsed += new ElapsedEventHandler(timer1_Elapsed);
          timer1.Interval = 60000;
          timer1.Enabled = true;
          timer1.AutoReset = true;
          timer1.Start();
  
          FileInfo f = new FileInfo(filePath);
          long s1 = f.Length;
  
          using (StreamWriter file = new StreamWriter(logPath))
          {
              file.WriteLine(s1.ToString());
          }
      }
  
      protected override void OnStop()
      {
          timer1.Enabled = false;
      }
  
      public void timer1_Elapsed(object sender, ElapsedEventArgs e)
      {
          FileInfo f = new FileInfo(filePath);
          long s2 = f.Length;
  
          using (StreamWriter file = new StreamWriter(newLog))
          {
              file.WriteLine(s2.ToString());
          }
      }
  }
  </code></pre>
  
  <p>What I can not figure out is how to reference s1 in the timer_elapsed method.  I tried declaring it globally but it would not work.  I want to just be able to do something like this in the timer elapsed method. </p>
  
  <pre><code>        if (s1 == s2)
          {
              //send email
          }
  
          else
          {
              //don't do anything
          }
  </code></pre>
  
  <p>Any advice would be much appreciated.</p>