【发布时间】:2011-09-07 05:29:17
【问题描述】:
我正在使用FileSystemWatcher 监视文件夹,它似乎阻止了文件夹的父 被删除,但不阻止文件夹本身被删除。
例如,我有文件结构:
C:\Root\FolderToWatch\...
FileSystemWatcher 以 FolderToWatch 为目标。在我的程序运行时,如果我转到 Windows 资源管理器并尝试删除 Root,我会收到错误消息“无法删除 Root:访问被拒绝”。
但是,如果我先删除FolderToWatch,我可以毫无意外地删除Root。
如果你想玩,这里有一些代码:
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');
}
为什么FileSystemWatcher 会像这样挂在它的目标父级上,而不是目标本身?
【问题讨论】: