开发者

C# File Monitoring

开发者 https://www.devze.com 2023-03-18 04:30 出处:网络
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

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.WriteL开发者_运维问答ine(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.


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消