简体   繁体   English

监视文件的更改

[英]Monitor a file for changes

I have been searching the web for good ideas on creating an application.我一直在搜索 web 以获得有关创建应用程序的好主意。 service, or vb script that will constantly monitor a file for changes.服务或 vb 脚本,它将不断监视文件的更改。 However, I want it to alert me when there are NO changes.但是,我希望它在没有变化时提醒我。 The file I am monitoring is a log created by a very important application.我正在监视的文件是由一个非常重要的应用程序创建的日志。 If that file stops growing then that means there is something wrong with the application and it has stopped writing to the log file.如果该文件停止增长,则意味着应用程序出现问题并且它已停止写入日志文件。

Does anyone have any ideas?有没有人有任何想法? I have played around with the FileSystemWatcher class in C# but I don't know if there is a way to use to alert when a file does not change since all of the events look at changes.我玩过 C# 中的 FileSystemWatcher class 但我不知道是否有一种方法可以在文件没有更改时发出警报,因为所有事件都在查看更改。

Thank you.谢谢你。

Just use a timer that fires on an interval that that file should be updated in.只需使用一个在该文件应该更新的时间间隔内触发的计时器。

Check the file size ( FileInfo.Length ) and if it is the same as the last time the timer fired, it is not growing.检查文件大小( FileInfo.Length ),如果它与上次触发计时器的时间相同,则它没有增长。

In this case you don't need a watcher, you could use a timer.在这种情况下,您不需要观察者,您可以使用计时器。 Every few minutes (or whatever fits your needs), check the file size.每隔几分钟(或任何适合您的需要),检查文件大小。 Store the last size somewhere.将最后一个尺寸存储在某处。 The next time you check it, compare the previous value to the current one.下次检查时,将之前的值与当前值进行比较。 If they are the same then the file has not changed and you can issue your alert.如果它们相同,则文件没有更改,您可以发出警报。

A file doesn't change most of the time.文件大部分时间都不会改变。 You need to define a time period beyond which the logging process is considered to have stopped.您需要定义一个时间段,超过该时间段记录过程被视为已停止。 Start a timer that (when it completes) will signal that logging has stopped.启动一个计时器(当它完成时)将发出记录已停止的信号。 Reset the timer every time a change event rolls in.每次发生更改事件时重置计时器。

You can use the FileSytemWatcher class:您可以使用FileSytemWatcher class:
http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.aspx http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.aspx

update: well, it pays to read the question through to the end... You can set a Timer ( ref ) for the duration of inactivity you want to be alerted of and reset the timer in FileSystemWatcher events.更新:嗯,将问题通读到最后是值得的......您可以为您希望收到警报的不活动持续时间设置一个Timerref ),并在FileSystemWatcher事件中重置计时器。

I suggest you to combine all answers you already got.我建议你结合你已经得到的所有答案。

Use FileSytemWatcher class to watch for changes and use a timer to wait for some period of time since last change .使用 FileSytemWatcher class 观察变化并使用计时器等待自上次变化以来的一段时间。

The simplest solution will look like ManualResetEvent and DateTime variables (don't forget memory barriers) and a background thread.最简单的解决方案看起来像 ManualResetEvent 和 DateTime 变量(不要忘记 memory 屏障)和后台线程。

So, you'll have a something like this:所以,你会有这样的东西:

DateTime _lastChangeTime;
ManualResetEvent _exitEvent; // set this event to exit thread.

private void Watcher_Change(...)
{
    _lastChangeTime = DateTime.UtcNow;
    Thread.MemoryBarrier();
}


private void ThreadProc()
{
    while (_exitEvent.Wait(1000)) // every second
    {
        Thread.MemoryBarrier();

        if (DateTime.UtcNow.Subtract(lastChangeTime) > [some reasonable time])
        {
            // shit happens...
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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