我需要使用哪个FileSystemWatcher筛选器来查找新文件 [英] Which filter of FileSystemWatcher do I need to use for finding new files

查看:60
本文介绍了我需要使用哪个FileSystemWatcher筛选器来查找新文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,我知道FileSystemWatcher可以查看一个文件夹,并且如果该文件夹中的任何文件被更改,修改等,那么我们就可以处理它. 但是我不确定在我的场景中应该使用哪个过滤器和事件:监视文件夹,如果将文件添加到该文件夹​​,请执行XYZ ...所以在我的场景中,我不在乎是否更改了现有文件等等.应该忽略这些……仅当且仅当已将新文件添加到该文件夹​​时,才执行XYZ ...

So far I know that FileSystemWatcher can look into a folder and if any of the files inside that folder is changed,modifies,.etc... then we can handle it. But I am not sure which filter and event I should use in my scenario: Watch for a Folder, If a file is added to that folder, do XYZ ... So In my scenario I don't care if an existing file is changed,etc..those should be ignored...only do XYZ if and only if a new file has been added to that Folder...

在这种情况下,您建议使用哪个事件和过滤器?

Which event and filter do you recommended for this scenario?

推荐答案

设置观察者:

FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = "Blah";

watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
    | NotifyFilters.FileName;

watcher.Created += new FileSystemEventHandler(OnChanged);

watcher.EnableRaisingEvents = true;

然后实现FileCreated委托:

private void OnChanged(object source, FileSystemEventArgs e) {
    Console.WriteLine("File: " +  e.FullPath + " " + e.ChangeType);
}

这篇关于我需要使用哪个FileSystemWatcher筛选器来查找新文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆