[英]FileSystemWatcher fires event without the file name
I have a pet project I'm working on where the FileSystemWatcher is vexing me. 我有一个宠物项目,正在研究FileSystemWatcher困扰我的地方。
Here's the initialization code: 这是初始化代码:
for (var xx = 0; xx < _roots.Count; xx++)
{
var watcher = new FileSystemWatcher();
var root = _roots[xx];
watcher.Path = root;
// watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;
watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName;
watcher.Filter = "*.*";
watcher.Changed += new FileSystemEventHandler(OnChanged);
watcher.Created += new FileSystemEventHandler(OnChanged);
watcher.Deleted += new FileSystemEventHandler(OnChanged);
watcher.Renamed += new RenamedEventHandler(OnRenamed);
watcher.EnableRaisingEvents = true;
_rootWatchers.Add(watcher);
}
Let's say the root we're watching "c:\\root" and there's a sub directory "c:\\root\\subdir" that contains a file called "file1.txt". 假设我们正在监视的根目录为“ c:\\ root”,并且有一个子目录“ c:\\ root \\ subdir”,其中包含一个名为“ file1.txt”的文件。
The watcher is up and running and I delete "file1.txt". 观察程序已启动并正在运行,我删除了“ file1.txt”。 When the handler is called and examine the values of FileSystemEventArgs
. 调用处理程序时,检查FileSystemEventArgs
的值。
I expect for e.Name == "file1.txt"
and e.FullPath == "c:\\\\root\\\\subdir\\\\file1.txt
. 我期望e.Name == "file1.txt"
和e.FullPath == "c:\\\\root\\\\subdir\\\\file1.txt
。
The actual values are "subdir"
and "c:\\\\root\\\\subdir"
. 实际值为"subdir"
和"c:\\\\root\\\\subdir"
。
I'm sure it's something simple I missed in the documentation somewhere. 我敢肯定,这是我在某个地方的文档中错过的简单事情。
You are correct, the issue you are facing is practically one of forgetting to set a property. 您是正确的,您面临的问题实际上是忘记设置属性的问题之一。
If you set watcher.IncludeSubdirectories = true;
如果设置watcher.IncludeSubdirectories = true;
, you'll get notified about the file deletion even in deeper levels. ,即使在更深的层次上,您也会收到有关文件删除的通知。
In the default mode, the FileSystemWatcher
only records changes to the given directory. 在默认模式下, FileSystemWatcher
仅记录对给定目录的更改。 Sub-directories being modeled sort of directory entries similar to files, any additions/deletions inside them are just reported as changes directly to the sub-directories (you would see that if you checked the FileSystemEventArgs.ChangeType
property in the OnChanged
handler). 子目录被建模为类似于文件的目录条目,它们中的任何添加/删除都将直接报告为对子目录的更改(如果您在OnChanged
处理程序中选中FileSystemEventArgs.ChangeType
属性,将会看到)。
Even if you turn on sub-directories monitoring, you'll still get a change event ( FileSystemEventArgs.ChangeType = WatcherChangeTypes.Changed
) for the subdir
directory as it is also modified when you delete a file inside it. 即使打开子目录监视,您仍然会收到subdir
目录的更改事件( FileSystemEventArgs.ChangeType = WatcherChangeTypes.Changed
),因为在删除其中的文件时也会对其进行修改。 That is in addition to the deletion event for the file. 这是文件删除事件的补充。
My test code: 我的测试代码:
static void Main(string[] args)
{
var watcher = new FileSystemWatcher();
watcher.Path = @"C:\test_dir";
// watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;
watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName;
watcher.Filter = "*.*";
watcher.Changed += new FileSystemEventHandler(OnChanged);
watcher.Created += new FileSystemEventHandler(OnChanged);
watcher.Deleted += new FileSystemEventHandler(OnChanged);
watcher.Renamed += new RenamedEventHandler(OnRenamed);
watcher.IncludeSubdirectories = true;
watcher.EnableRaisingEvents = true;
while (true)
{
}
}
private static void OnRenamed(object sender, RenamedEventArgs e)
{
Console.WriteLine($"OnRenamed: {e.FullPath}, {e.OldFullPath}");
}
private static void OnChanged(object sender, FileSystemEventArgs e)
{
Console.WriteLine($"OnChanged: {e.ChangeType}, {e.Name}[{e.FullPath}]");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.