Powershell System.IO.FileSystemWatcher on network shares doesn't work when files are written remotely

只谈情不闲聊 提交于 2019-12-11 01:06:21

问题


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.

Example code:

$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)"
}

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


回答1:


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

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

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.)

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.




回答2:


It would appear from the c# sample here http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher%28v=vs.110%29.aspx that you need full trust on the code and to set a NotifyFilter = [System.IONotifyFilters]::DirectoryName



来源:https://stackoverflow.com/questions/20389702/powershell-system-io-filesystemwatcher-on-network-shares-doesnt-work-when-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!