Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FileSystemWatcher files in subdirectory

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).

like image 316
Josh Avatar asked Apr 30 '13 13:04

Josh


3 Answers

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);
        }
    }
}
like image 81
Alex Filipovici Avatar answered Oct 18 '22 02:10

Alex Filipovici


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.

like image 39
Marcel N. Avatar answered Oct 18 '22 02:10

Marcel N.


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

like image 1
Darko Kenda Avatar answered Oct 18 '22 02:10

Darko Kenda