LastWrite FileSystemWatcher Powershell:通知
我想在某个文件发生更改时收到通知。它工作正常,但我无法设置 NotifyFilter。 “未被识别为 cmdlet 的名称...” 主要问题是:我的脚本在我的函数中运行了两次。确定了更改,然后我得到了两次“更改”
function fileChanged ($text) {
Write-Host $text
}
$watcher = New-object System.IO.FileSystemWatcher "C:\Users\test\Desktop\"
$watcher.EnableRaisingEvents = $true
$watcher.Filter="*.txt"
$watcher.NotifyFilter = (System.IO.NotifyFilters.LastWrite)
$changed = Register-ObjectEvent $watcher "Changed" -Action {
$txt = "changed"
filechanged($txt)
}
while($true) {
echo "wartet"
$null = $watcher.WaitForChanged("Changed")
start-sleep -s 2
}
I would like to get an Notification when a certain file where changed. It works fine but I can't set the NotifyFilter. "is not recognized as the name of a cmdlet..."
The main problem is: That my script goes twice in my function. A change is identified and then I get two times "changed"
function fileChanged ($text) {
Write-Host $text
}
$watcher = New-object System.IO.FileSystemWatcher "C:\Users\test\Desktop\"
$watcher.EnableRaisingEvents = $true
$watcher.Filter="*.txt"
$watcher.NotifyFilter = (System.IO.NotifyFilters.LastWrite)
$changed = Register-ObjectEvent $watcher "Changed" -Action {
$txt = "changed"
filechanged($txt)
}
while($true) {
echo "wartet"
$null = $watcher.WaitForChanged("Changed")
start-sleep -s 2
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在声明notifyfilter的代码中更改此内容:
但这并不能解决问题,请阅读以下内容:
来自 VS.NET 文档的“FileSystemWatcher 组件故障排除” 部分...
为单个操作生成多个创建的事件
您可能会注意到,在某些情况下,单个创建事件会生成多个由组件处理的 Created 事件。例如,如果您使用 FileSystemWatcher 组件监视目录中新文件的创建,然后使用记事本创建文件进行测试,则即使只创建了一个文件,您也可能会看到生成了两个 Created 事件。这是因为记事本在写入过程中执行多个文件系统操作。记事本批量写入磁盘,创建文件内容,然后创建文件属性。其他应用程序可以以相同的方式执行。由于 FileSystemWatcher 监视操作系统活动,因此将拾取这些应用程序触发的所有事件。
注意:记事本还可能导致其他有趣的事件生成。例如,如果您使用 ChangeEventFilter 指定您只想监视属性更改,然后使用记事本写入正在监视的目录中的文件,则会引发事件 。这是因为记事本在此操作期间更新了文件的已存档属性。
change this in your code for declaring notifyfilter:
But this not solve the issue, read this:
From the "Troubleshooting FileSystemWatcher Components" section of the VS.NET documentation...
Multiple Created Events Generated for a Single Action
You may notice in certain situations that a single creation event generates multiple Created events that are handled by your component. For example, if you use a FileSystemWatcher component to monitor the creation of new files in a directory, and then test it by using Notepad to create a file, you may see two Created events generated even though only a single file was created. This is because Notepad performs multiple file system actions during the writing process. Notepad writes to the disk in batches that create the content of the file and then the file attributes. Other applications may perform in the same manner. Because FileSystemWatcher monitors the operating system activities, all events that these applications fire will be picked up.
Note: Notepad may also cause other interesting event generations. For example, if you use the ChangeEventFilter to specify that you want to watch only for attribute changes, and then you write to a file in the directory you are watching using Notepad, you will raise an event . This is because Notepad updates the Archived attribute for the file during this operation.
WaitForChanged
是显式使用FileSystemWatcher
来同步等待更改。启用RaisingEvents
正在执行异步等待:当某些内容发生变化时,将调用事件处理程序。您同时以同步和异步方式查找更改。选择一个并使用它,同时使用两个会使一切变得混乱。
WaitForChanged
is explicitly using theFileSystemWatcher
to synchronously wait for changes.EnableRaisingEvents
is performing an asyncrhonous wait: the event handlers will be called when something changes.You are simultaneously looking for changes synchronously and asynchronously. Chose one and use that, using both is going to confuse everything.
发现以下是最简单的:
归功于 Derek Newton,网址为:
http://dereknewton.com/2011/05/monitoring-文件系统更改与 powershell/
Found the following was the most simple:
Credit to Derek Newton at:
http://dereknewton.com/2011/05/monitoring-file-system-changes-with-powershell/