FileSystemWatcher files in subdirectory

后端 未结 3 1584
借酒劲吻你
借酒劲吻你 2021-01-04 20:40

I\'m trying to be notified if a file is created, copied, or moved into a directory i\'m watching. I only want to be notified about the files though, not the directories.

3条回答
  •  一个人的身影
    2021-01-04 20:52

    Add some more filters to your NotifyFilters. At the moment you're only watching for changes in file names. That, together with your Changed and Renamed handlers should do the job.

    _watcher.NotifyFilter = NotifyFilters.FileName | NotifyFilters.LastAccess | NotifyFilters.LastWrite
    

    This seems to be working only for copy/paste actions. For cut/paste actions (or drag&drop), add the following notify filter too: NotifyFilters.DirectoryName.

    EDIT

    I've played around with it a bit more and indeed only one notification for the top level folder comes in. Makes sense, if you come to think of it. Since the change type is created then you know for sure that all files and folders inside it are new and you can process them.

    So, @AlexFilipovici's approach is the only viable one, although I'd enqueue the result (folder) and process it on a worker thread (or task, whatever). You don't want to spend too much time inside a FSWatcher event handler, especially if files are coming in at a high rate.

提交回复
热议问题