FileSystemWatcher IncludeSubdirectories在网络共享上不起作用 [英] FileSystemWatcher IncludeSubdirectories not working on network share

查看:91
本文介绍了FileSystemWatcher IncludeSubdirectories在网络共享上不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试查看网络共享中是否有文件夹,其中已将文件添加到共享中以指示上传已完成.我想使用FileSystemWatcher来监视该触发器文件的共享及其子目录,如下所示.

I'm attempting to watch a network share for folders where a file is added to the share to indicate the upload is complete. I wanted to use a FileSystemWatcher to monitor that share and its subdirectories for that trigger file, as shown below.

FileSystemWatcher fsw = new FileSystemWatcher(share, triggerFilePattern);
fsw.IncludeSubdirectories = true;
fsw.Created += new FileSystemEventHandler(OnCreated);
fsw.Renamed += new RenamedEventHandler(OnRenamed);
fsw.Error += new ErrorEventHandler(OnError);

如果在共享的根目录下创建了文件,则事件将触发.如果在共享的子目录中创建了文件,则该事件不会触发 not ,并且我也不会收到错误.

If a file is created at the root of the share, the event triggers. If a file is created within a subdirectory of the share, the event does not trigger, and I don't get an error either.

如果我为该子目录创建了一个新的FileSystemWatcher,那么当在那里创建文件时,我确实会收到一个事件.但是,类似于顶级FileSystemWatcher,在任何其他子目录中创建的文件,我都不会得到事件.

If I create a new FileSystemWatcher for that subdirectory, then I do receive an event when a file is created there. But, similar to the top level FileSystemWatcher, I won't get events for files created in any further subdirectories.

如果我将网络共享更改为本地目录以进行测试,则它确实可以正常工作.

If I change the network share to a local directory for testing, it does work as expected.

有什么想法吗?我可以设置一些不可靠的网络共享设置来阻止递归FileSystemWatcher检查吗?我可以解决此问题,但是不必使代码复杂化就很好.

Any ideas? Could I have set up some wonky network share setting that blocks recursive FileSystemWatcher checks? I can work around this, but it would be nice to not have to complicate the code.

我看到我在属性"->安全性"选项卡下没有完全控制",所以我认为可能是这样.但是我能够在具有相同可见权限的其他共享上获得正常的预期行为,所以我回到不知道为什么该特定共享不起作用的原因.

I saw that I didn't have Full Control under Properties->Security tab, so I thought that could be it. But I was able to get normal desired behavior on a different share with the same visible permissions, so I'm back to not knowing why this specific share isn't working.

Edit2:在同事的建议下,我也添加了一个Changed处理程序.对于我来说,没有先创建就如何更改文件是没有意义的,但是...在子目录中创建文件时,正在共享该共享上的Changed事件. (而且重命名它们时我仍然什么也没得到.)这解决了我眼前的问题,但是如果有人能够回答为什么为什么,我将把问题悬而未决.

At a coworker's suggestion, I added a Changed handler too. Doesn't make sense to me how a file can be changed without getting Created first, but... I'm getting Changed events on the share in question when creating files in a subdirectory. (And I'm still getting nothing when renaming them.) This solves my immediate problem, but I'm going to leave the question open in case someone's able to answer why that's happening.

推荐答案

尝试一下

 private static FileSystemWatcher fw;
 static void Main(string[] args)
       {
            fw = new FileSystemWatcher(@"D:\Folder_Watch");
            fw.IncludeSubdirectories = true;
            fw.NotifyFilter = NotifyFilters.LastAccess |
     NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;
            fw.Created += fw_Created;
            fw.Changed += fw_Created;
            fw.Renamed += fw_Created;
            fw.Filter = "*.*";
            fw.EnableRaisingEvents = true;

            new System.Threading.AutoResetEvent(false).WaitOne();        
        }

 static void fw_Created(object sender, FileSystemEventArgs e)
        {
            try
            {
                fw.EnableRaisingEvents = false;
                //Your Code
            }
            finally
            {
                fw.EnableRaisingEvents = true;
            }
        }

这篇关于FileSystemWatcher IncludeSubdirectories在网络共享上不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆