FileSystemWatcher supports parent directory

I use FileSystemWatcherfolders to track, and it seems to prevent the parent folders from being deleted, but not the folder itself.

For example, I have a file structure:

C:\Root\FolderToWatch\...

with targeting FileSystemWatcher FolderToWatch. While my program is running, if I go to Windows Explorer and try to uninstall Root, I get the error message "Unable to delete root: access is denied."

However, if I remove FolderToWatchFIRST, I can delete it Rootwithout incident.

Here is the code if you want to play with it:

static void Main(string[] args) {

    var watcher = new FileSystemWatcher(@"C:\Root\FolderToWatch");

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

    watcher.Changed += (sender, e) => Console.WriteLine(e.FullPath);
    watcher.Created += (sender, e) => Console.WriteLine(e.FullPath);
    watcher.Deleted += (sender, e) => Console.WriteLine(e.FullPath);
    watcher.Renamed += (sender, e) => Console.WriteLine(e.FullPath);

    watcher.EnableRaisingEvents = true;

    Console.WriteLine("Press \'q\' to quit.");
    while (Console.Read() != 'q');
}

Why FileSystemWatcherdoes the target parent hang on it, but not the object itself?

+3
source share
1

, , , , "FolderToWatch", FileSystemWatcher.

!

+1

Source: https://habr.com/ru/post/1748233/


All Articles