当远程写入文件时,网络共享上的Powershell System.IO.FileSystemWatcher不起作用 [英] Powershell System.IO.FileSystemWatcher on network shares doesn't work when files are written remotely

查看:237
本文介绍了当远程写入文件时,网络共享上的Powershell System.IO.FileSystemWatcher不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要查看何时将文件写入目录.该目录位于Windows 2003服务器上的映射网络驱动器上.如果我将文件复制到服务器上的该目录,则可以正常工作.如果我远程写入文件,它将无法正常工作.当远程写入文件时,我该怎么做才能使其正常工作?我想将其保留为事件通知,但是如果需要,将更改为一种轮询方法.如果那是正确的方法,那么我需要一些最佳实践建议.我可以提供有关Windows的文件写入通知如何工作的任何详细信息.

I need to see when a file is written to a directory. This directory is on a mapped network drive on a Windows 2003 server. If I copy files to this directory on the server, it works. If I write files remotely, it does not work. What can I do to make it work when files are written remotely? I would like to keep this as an event notification, but will change to a polling method if needed. If thats the correct way to do it, I need some best practice advice. Any detail I can get on how Windows' file write notifications work is welcome.

示例代码:

$watchFolder = "Z:\watched";
$filter = "*.data";
$watcher = New-Object System.IO.FileSystemWatcher $watchFolder, $filter
$watcher.EnableRaisingEvents = $true

$created = Register-ObjectEvent $watcher Created -Action {
   write-host "Found: $($eventArgs.FullPath)"
}

我的经验水平:咸的Unix管理员,是Windows开发的新手.

My experience level: salty unix admin, new to Windows development.

推荐答案

FileSystemWatcher中有两种过滤器"类型的规范,即过滤器"字符串和"NotifyFilter"参数.

There are two "filter"-type of specifications in FileSystemWatcher, the "Filter" string and the "NotifyFilter" parameter. The $filter parameter that you pass to the ctor in the line

$watcher = New-Object System.IO.FileSystemWatcher $watchFolder, $filter

仅设置过滤器"字符串,该字符串仅为观察者确定名称"过滤器.您还需要设置NotifyFilter参数以指定要监视的更改类型.可以在同一行上完成:

only sets the "filter" string, which determines only a "name" filter for the watcher. You also need to set the NotifyFilter parameter to specify which types of changes you are watching for. This can be done on the same line:

$watcher = New-Object System.IO.FileSystemWatcher $watchFolder,$filter  -Property @{IncludeSubdirectories = $false; InternalBufferSize = 16384; EnableRaisingEvents = $true; NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'}

我还输入了一些其他应设置的属性. "InternalBufferSize"是用于修复错误的技巧-我不记得该错误的详细信息,但最好将其放入(有关详细信息,请参阅Google.)

I have also put in some other properties you should set. The "InternalBufferSize" is a hack to fix a bug - I can't remember the details of the bug, but best to put it in (google for details.)

顺便说一句,即使进行了这些修复,我也遇到了类似的问题:监视网络驱动器上的文件(无论是否已将其映射并使用该映射进行访问)都是不稳定的.另外,如果您正在监视的目录在监视期间消失(并且可能重新出现),则filesystemwatcher中存在一些已知的错误-除非指定了超时,否则filesystemwatcher不会恢复.

I am, btw, having similar issues to you, even with these fixups: watching for files on a network drive (regardless of whether or not you have it mapped and use that mapping for access) is flaky. Also, there are known bugs in filesystemwatcher if the directory you are watching disappears (and potentially re-appears) during the watch - filesystemwatcher will not recover unless a timeout has been specified.

因此,也许我的更改会解决您的问题,也许不会.如果您发现的问题解决方案与我在此处发布的解决方案有所不同,那么您是否愿意发布,因为即使我发布的内容也不足以解决我的问题,这听起来与您的问题类似.谢谢.

So, perhaps my changes will fix your problem, perhaps not. If you found a fix for your problem that differs from what I have posted here, would you mind posting, because even what I have posted is not sufficient to fix my problems, which sound similar to your problems. Thanks.

这篇关于当远程写入文件时,网络共享上的Powershell System.IO.FileSystemWatcher不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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