如何使用FileSystemWatcher监视文件大小更改?

时间:2015-07-29 09:04:14

标签: c# .net filesystemwatcher

设置观察者的方法:

private void WatchDirectory()
        {
            FileSystemWatcher watcher = new FileSystemWatcher();
            watcher.Path = userVideosDirectory;
            watcher.NotifyFilter = NotifyFilters.LastWrite;
            watcher.Filter = "*.mp4";
            watcher.Changed += new FileSystemEventHandler(OnChanged);
            watcher.EnableRaisingEvents = true;
        }

该事件告知是否有任何变化:

private void OnChanged(object source, FileSystemEventArgs e)
    {
        dirchanged = true;
    }

现在我正在检查目录中是否有新文件。 但现在我还想观察并检查这个新的最后创建的文件大小是否在变化。当文件的大小不再改变时,给出一条消息,如“文件准备就绪”。

如何实时查看文件大小,直到没有更多更改?

1 个答案:

答案 0 :(得分:1)

使用文件系统在两个应用程序之间进行通信时,这是一个非常常见的问题。根据我的经验,如果您还有一个标记文件指示“真实”文件是完整写入的,那么它最有效:

SystemA: Writes file theRealFile.txt
SystemA: Writes file theRealFile.rdy (0byte)
SystemB: Watches for .rdy files and then reads theRealFile.txt