C # FileSystemWatcher.Deleted no dispara en la eliminación "normal"?

Código:

FileSystemWatcher fileSystemWatcher = new FileSystemWatcher(path, "*.exe");
fileSystemWatcher.IncludeSubdirectories = true;
fileSystemWatcher.Created += new FileSystemEventHandler(fileSystemWatcher_Created);
fileSystemWatcher.Deleted += new FileSystemEventHandler(fileSystemWatcher_Deleted);
fileSystemWatcher.EnableRaisingEvents = true;

The Created Event works fine, but the Deleted Event is only firing, when Deleting a Directory/or Exe with BASICO. But normal-delete (moving to recycle bin) isn't working/firing the event!

¿Como resolver el problema?

preguntado el 08 de noviembre de 11 a las 09:11

Does the event fire when you empty the recycle bin after deleting a file? -

nope, nothing happens, only when deleting the file holding SHIFT -

Mh, makes sense. Deleting it means moving it to the recycle bin which is a Rename event. After that the file is outside of the path you are watching. -

Moving/Renaming a file to the recycle bin is not deleting. That you click "Delete" to move a file to the recycle bin is purely a shell abstraction, which the file system doesn't know about. -

Weird, it works for me as it should. It fires when files are deleted or "shift + delete". -

4 Respuestas

I know it's an old question, but I resolved this by adding FileName to the NotifyFilter property of the FileSystemWatcher object.

Respondido 26 ago 16, 09:08

Doesn't work for me. No matter how I delete (w/ or w/o Shift) it doesn't fire at all. Strange.. - IgorStack

@IgorStack; did you set EnableRaisingEvents to true? - AlbertVanHalen

This is expected behaviour as the file isn't actually deleted: it's moved.

Try attaching to

filesystemWatcher.Renamed

and checking if the file is moved to the Recycle Bin instead.

Finding where the recycle bin actually is in the filesystem is not trivial, mind you. Some code posted by others (untried) is here: http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/5d2be9aa-411c-4fd1-80f5-895f64aa672a/ - and also here: ¿Cómo puedo saber que un directorio es la papelera de reciclaje en C #?

contestado el 23 de mayo de 17 a las 14:05

thats clear and I know this, but how to solve the problem? the object doesn't have a moving event! - eMi

To expand on this, the recycle bin is actually just a special system directory, usually C:\$RECYCLER or C:\$Recycle.Bin. It aggregates soft-deleted files into a special tree structure, which can then allow restoration or permenant deletion of those files. - Polinomio

@JeremyMcGee Interesting, just for clarification, what happens if they shift-delete? - Adam Houldsworth

In order to detect the movement, you could use the Changed event. When you move a file, it is marked as changed. You should be able to then check if the file is still there using File.Exists - Polinomio

@eMi: check to see if the file is actually there - Jeremy McGee

The solution is to use the following code:

private static void EnableFileWatcherLvl1(string folder, string fileName)
{
    FileSystemWatcher watcher = new FileSystemWatcher();
    watcher.Path = folder;
    watcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.Attributes;
    watcher.Filter = "*.txt";
    watcher.Changed += watcher_Changed;
    watcher.Deleted += watcher_Changed;
    watcher.EnableRaisingEvents = true;
}

static void watcher_Changed(object sender, FileSystemEventArgs e)
{            
    switch (e.ChangeType)
    {
        case WatcherChangeTypes.Changed: { // code here for created file }
             break;
        case WatcherChangeTypes.Deleted: { // code here for deleted file }
             break;
    }       
}

Presta atención a NotifyFilter property, those are the filters it has to use. And this will trigger for Added y Delete files. I tested this in.Net Framework 4.5.

contestado el 06 de mayo de 19 a las 20:05

This one will Work.

FileSystemWatcher fsw = new FileSystemWatcher(folderPath);
        fsw.Deleted+= FileSystem_Deleted;
        fsw.EnableRaisingEvents = true;
        fsw.IncludeSubdirectories = true;

static void FileSystem_Deleted(object sender, FileSystemEventArgs e)
    {
        // Write your code
       MessageBox.Show("Deleted Item is " + e.name);

    }

Respondido 10 Abr '21, 20:04

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.