FileSystemWatcher Service doesnot detect changes when folder containing the file is deleted.

I am using the FileSystemWatcher Service to monitor xml files within sub folders in a root folder. The problem that I am having is that when I delete the folder within the root folder which contains the xml file, the delete event does not seem to be fired. It works fine when I delete the xml file itself. here is the code. Please let me know what I am doing wrong.

watchfolder.Path=WEBDest;

watchfolder.IncludeSubdirectories = true;

watchfolder.Filter="*.xml";

//watchfolder.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite

//| NotifyFilters.FileName | NotifyFilters.DirectoryName | NotifyFilters.Size | NotifyFilters.Attributes;

watchfolder.Created += new FileSystemEventHandler(this.FileChanged);

watchfolder.Changed += new FileSystemEventHandler(this.FileChanged);

watchfolder.Deleted += new FileSystemEventHandler(this.FileChanged);

Smitha




Answer this question

FileSystemWatcher Service doesnot detect changes when folder containing the file is deleted.

  • KCtin

    Thanks Henry. That helped me understand the fsw behavior better. But, this will not work in my case. I am monitoring a large number of xml files within almost 100 folders. It is very difficult to keep track of all the folders and the xml files within these folders.The other thing is that I am using this in a windows service.

    Thanks,
    Smitha



  • orac123456789

    Maybe the solution is to have two services :

    One is watching the arrival of new XML files. Each time you receive an event for a new XML file, you add an entry (ie the full path of the file) in a SQL Express 2005 datatable.

    The other one monitors the datable each time it receives a deleted folder event. You just have to check for each file in the datatable if it exists or not.



  • ChadLee

    hi smitha

    you missed watchfolder.EnableRaisingEvents = true;

    sai



  • SHELMAN

    hi smitha

    i tried with this code, it is working fine, and i checked through lan also

    FileSystemWatcher fs = new FileSystemWatcher(@"C:\SAI");

    fs.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;

    fs.EnableRaisingEvents = true;

    fs.Filter = "*.txt";

    fs.Deleted += new FileSystemEventHandler(fs_Deleted);

    fs.Created += new FileSystemEventHandler(fs_Created);

    fs.Renamed += new RenamedEventHandler(fs_Renamed);

    fs.IncludeSubdirectories = true;

    void fs_Created(object sender, FileSystemEventArgs e)

    {

    MessageBox.Show(e.Name + " is Created");

    }

    void fs_Deleted(object sender, FileSystemEventArgs e)

    {

    MessageBox.Show(e.Name + " is Deleted");

    }

    void fs_Renamed(object sender, RenamedEventArgs e)

    {

    MessageBox.Show(e.OldName + " to " + e.Name);

    }

    sai



  • SUPAXE

    Thanks for the reply. I actually have watchfolder.EnableRaisingEvents=true; in my code. I just forgot to add it here. The service works fine when I delete the xml file itself. However, if the folder containing the file is deleted, it does not work. I am not sure how the file system behaves when a folder is deleted. Does it not raise events for the files within the folder

    Here is what I am trying to do. I have several subfolders within my WEBPath folder. All these subfolders contain xml files. If the xml files are deleted, the events are detected. However, if the subfolder is deleted, no event is raised for the xml file.

    I hope someone can help me with this.

    Thanks in advance,
    Smitha



  • Dhanasu

    Thanks for the reply. My code is exactly the same. I am monitoring the files in the path "c:\smitha". I have a folder "c:\smitha\testfolder". I have xml file within testfolder "c:\smitha\testfolder\test.xml". I have set the filesystemwatcher filter to *.xml and includesubdirectories to true.

    Everything is fine when i delete the test.xml. I get the required results. however, if I delete testfolder itself (which includes the test.xml), no events are fired. If I remove the filter for xml files, I noticed that the events are triggered for "c:\smitha" and "c:\smitha\testfolder" when testfolder is deleted. However, i would like to keep the filter to xml.

    It looks to me like if I delete a folder containing files, the events are triggered only for the folder and not for the files within the folder. So, if I filter for xml files, no events are triggered (because they are folders). And if I remove the filter, events are triggered for the folders. However, I need to know the files which are deleted. Is there a way to do this

    Thanks,

    Smitha



  • Kent Chenery

    Dear Smitha,

    my feeling is that if you have both notify filters on FileName and DirectoryName, the deleted event for DirectoryName hides all events on subfolders and files within that deleted folder. I have spent some times in the MSDN literature and the situation about the deleted event of the FSW Class is not so clear.

    What I suggest is that you remove the DirectoryName from the Notify filter enumeration, and maybe add the LastAccess on file to the notify filter.

    I hope it will help



  • Amit Tzafrir

    That is exactly what I plan to do. I was just hoping there was some setting in fsw which could make it simpler for me.

    Thanks,
    Smitha



  • Timpie

    Hi Henri,

    Thanks for the prompt reply. I removed the DirectoryName from the Notify filter enumeration. But, it still did not trigger any event. It almost seems like the delete event does not get propogated to the file level if the folder is deleted. Please let me know if anybody has any suggestions.

    Thanks,

    Smitha



  • NickNotYet

    Dear Smitha,

    I you observe the behavior of the trash on your desktop, you will see that each time you delete a folder, you can see that folder in the trash but you cannot see the elements within that dropped folder. The only way to see those elements is to recover the folder from the trash.

    I think it happens the same thing with the FSW class. When you delete a folder inside a watched directory you only have the event of the deleted folder because the folder and its contents is not really deleted but only moved to the trash. This is why you never receive the deleted events for the included files because they are still somewhere on your system.

    The idea should be to monitor the trash itself with the FSW class.

    But the problem is : where is located that trash

    On XP is seems to be in a hidden folder located at the root of the partition and is called "recycled". For example if your watched directory is on C:\, you should monitor the C:\Recycled folder. If you do so you will be notified for the deleted file when the trash is emptied by the user.

    If you setup the Trash to automatically drop the files without asking a confirmation, then you will receive the deleted events for all included files within the deleted folder. But this situation is risky, because you will be unabled to recover the dropped files.

    What I suggest is that you implement your own recycled mechanism in the class where you use the FSW object : each time a file is created, you should store its full path in an XML file and you should maintain the directory tree structure in this XML file. Each time you receive a delete event for a folder, you will be able to list all files that were in that folder at the time you receive the notification.



  • FileSystemWatcher Service doesnot detect changes when folder containing the file is deleted.