Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6125821
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:17:32+00:00 2026-05-23T16:17:32+00:00

This applies to a previous question I had asked. I have been trying to

  • 0

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

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());
        }
    }
}

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.

        if (s1 == s2)
        {
            //send email
        }

        else
        {
            //don't do anything
        }

Any advice would be much appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T16:17:33+00:00Added an answer on May 23, 2026 at 4:17 pm

    just do it like this:

    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";
        long oldFileSize; // Add this line
    
        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);
            oldFileSize = f.Length; // change this line
    
            using (StreamWriter file = new StreamWriter(logPath))
            {
                file.WriteLine(s1.ToString());
            }
        }
    
        public void timer1_Elapsed(object sender, ElapsedEventArgs e)
        {
            FileInfo f = new FileInfo(filePath);
            long s2 = f.Length;
    
            using (StreamWriter file = new StreamWriter(newLog))
            {
                if (s2 == oldFileSize)
                {
                    // Send Email
                }
                file.WriteLine(s2.ToString());
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using IntelliJ IDEA 9.0.2 but I assume this question applies to previous versions
I've asked this before for Java , but this applies specifically to J2ME. Is
I believe this question applies to any of the For Html helpers, but my
I believe this question applies equally well to C# as to Java, because both
I am using PrimeFaces p:messages but I think this question applies equally to h:messages
I asked a similar question before ( integer-vs-char-for-db-record-property ) but stumbled upon something that
In a previous question, I asked how to send text to Notepad . It
The previous question I asked about creating a DIV where size is calculated from
This is closely related to my previous question , which was about using CMake
This is a more focused version of a previous question of mine around Sinatra's

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.