FileSystemWatcher files in subdirectory

╄→尐↘猪︶ㄣ 提交于 2019-11-30 03:57:51

问题


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.

Here's some of the code I currently have:

_watcher.NotifyFilter = NotifyFilters.FileName;
_watcher.Created += new FileSystemEventHandler(file_created);
_watcher.Changed += new FileSystemEventHandler(file_created);
_watcher.Renamed += new RenamedEventHandler(file_created);
_watcher.IncludeSubdirectories = true;
_watcher.EnableRaisingEvents = true;

Problem is, if I move a directory that contains a file in it, I get no event for that file.

How can I get it to notify me for all files added (regardless of how) to the watched directory or it's sub directories?

Incase I didn't explain good enough... I have WatchedDirectory, and Directory1. Directory1 contains Hello.txt. If I move Directory1 into WatchedDirectory, I want to be notified for Hello.txt.

EDIT: I should note my OS is Windows 8. And I do get notification for copy/paste events, but not move events (drag and drop into the folder).


回答1:


Maybe this workaround could come in handy (but I'd be careful about performance as it involves recursion):

private static void file_created(object sender, FileSystemEventArgs e)
{
    if (e.ChangeType == WatcherChangeTypes.Created)
    {
        if (Directory.Exists(e.FullPath))
        {
            foreach (string file in Directory.GetFiles(e.FullPath))
            {
                var eventArgs = new FileSystemEventArgs(
                    WatcherChangeTypes.Created,
                    Path.GetDirectoryName(file),
                    Path.GetFileName(file));
                file_created(sender, eventArgs);
            }
        }
        else
        {
            Console.WriteLine("{0} created.",e.FullPath);
        }
    }
}



回答2:


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.




回答3:


Copying and moving folders

The operating system and FileSystemWatcher object interpret a cut-and-paste action or a move action as a rename action for a folder and its contents. If you cut and paste a folder with files into a folder being watched, the FileSystemWatcher object reports only the folder as new, but not its contents because they are essentially only renamed.

Reference: MSDN



来源:https://stackoverflow.com/questions/16301598/filesystemwatcher-files-in-subdirectory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!