gpt4 book ai didi

c# - FileSystemWatcher IncludeSubdirectories 不适用于网络共享

转载 作者:太空宇宙 更新时间:2023-11-03 15:24:46 26 4
gpt4 key购买 nike

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

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

如果在共享的根目录下创建了一个文件,事件就会触发。如果在共享的子目录中创建文件,则事件不会触发,我也不会收到错误。

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

如果我将网络共享更改为本地目录进行测试,它会按预期工作。

有什么想法吗?我可以设置一些不稳定的网络共享设置来阻止递归 FileSystemWatcher 检查吗?我可以解决这个问题,但最好不要使代码复杂化。

编辑:我看到我在“属性”->“安全”选项卡下没有“完全控制”,所以我认为可能就是这样。但是我能够在具有相同可见权限的不同共享上获得正常的期望行为,所以我又不知道为什么这个特定共享不起作用。

Edit2:在同事的建议下,我也添加了一个 Changed 处理程序。对我来说没有意义如何在不先创建文件的情况下更改文件,但是......在子目录中创建文件时,我在相关共享上收到更改事件。 (重命名它们时我仍然一无所获。)这解决了我眼前的问题,但我将保留这个问题,以防有人能够回答为什么会发生这种情况。

最佳答案

试试这个

 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;
}
}

关于c# - FileSystemWatcher IncludeSubdirectories 不适用于网络共享,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35835090/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com