我如何使用 FileSystemWatcher 来监视文件大小的变化?
How can i use FileSystemWatcher to watch for file size changes?
设置观察者的方法:
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;
}
现在我正在检查目录中是否有新文件。
但现在我也想观察并检查这个新创建的文件大小是否发生变化。当文件的大小不再改变时,给出类似 "The file is ready".
的消息
如何实时查看文件大小,直到没有更多变化?
当使用文件系统在两个应用程序之间进行通信时,这是一个很常见的问题。根据我的经验,如果您还有一个标记文件表明 "real" 文件已完全写入,则效果最佳:
SystemA: Writes file theRealFile.txt
SystemA: Writes file theRealFile.rdy (0byte)
SystemB: Watches for .rdy files and then reads theRealFile.txt
设置观察者的方法:
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;
}
现在我正在检查目录中是否有新文件。 但现在我也想观察并检查这个新创建的文件大小是否发生变化。当文件的大小不再改变时,给出类似 "The file is ready".
的消息如何实时查看文件大小,直到没有更多变化?
当使用文件系统在两个应用程序之间进行通信时,这是一个很常见的问题。根据我的经验,如果您还有一个标记文件表明 "real" 文件已完全写入,则效果最佳:
SystemA: Writes file theRealFile.txt
SystemA: Writes file theRealFile.rdy (0byte)
SystemB: Watches for .rdy files and then reads theRealFile.txt