简体   繁体   中英

C# File Monitoring

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.

You need the FileSystemWatcher class. There's settings for NotifyFilter / Path / IncludeSubdirectories.

Also, this class raises a bunch of events for Changed, Created, Deleted, Renamed.

http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.aspx

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM