简体   繁体   English

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

[英]How can i use FileSystemWatcher to watch for file size changes?

A method that set the watcher: 设置观察者的方法:

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

And the event that tell if there is any changes: 并告诉您是否有任何更改的事件:

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

Now i'm checking if there is a new file in the directory. 现在,我正在检查目录中是否有新文件。 But now i also want to watch and check if this new last created file size changing. 但是现在我也想观察并检查这个新创建的文件大小是否正在更改。 And when the size of the file is not changing anymore give a message like "The file is ready". 并且当文件大小不再更改时,给出类似“文件已准备好”的信息。

How can i watch for the file size in real time untill there is no more changes ? 在没有更多更改之前,我如何实时查看文件大小?

This is quite a common problem when using the file system to communicate between two applications. 使用文件系统在两个应用程序之间进行通信时,这是一个非常普遍的问题。 In my experience it works best if you also have a marker file indicating that the "real" file was written completely: 以我的经验,如果您还有一个标记文件表明“真实”文件已完全写入,则效果最佳。

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

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

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