简体   繁体   English

FileSystemWatcher触发没有文件名的事件

[英]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.

相关问题 FileSystemWatcher在第二次更改后触发事件,但不是第一次 - FileSystemWatcher Fires Event After Second Change, But Not First 在Filesystemwatcher引发的事件中删除文件 - Deleting file in event raised by Filesystemwatcher FileSystemWatcher在文件保存之前启动-您如何“暂停”该过程? - FileSystemWatcher fires before file is saved - how do you “pause” the process? 使用c#窗口窗体在运行时编辑名称,FileSystemWatcher.Renamed事件重命名文件 - Rename a file at runtime edit name , FileSystemWatcher.Renamed Event using c# window form 我如何才能使filesystemwatcher触发每个文件创建的事件而不会丢失任何事件 - how can i enable filesystemwatcher to fire each and every file created event without loosing any FileSystemWatcher更改了最近打开的文件未触发的事件 - FileSystemWatcher changed event not firing for recently opened file FileSystemWatcher 无法检测到正确的文件名 - FileSystemWatcher can not detect right file name File.Exists()在FileSystemWatcher的Deleted事件中返回false - File.Exists() returns false in Deleted event of FileSystemWatcher 配置filesystemwatcher,使其仅在完全复制文件后引发创建的事件 - configuring filesystemwatcher such that it raises created event only when the file is fully copied 如何确定哪个文件过滤器与 FileSystemWatcher 事件匹配? - How can I determine which file filter was matched for a FileSystemWatcher event?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM