LastWrite FileSystemWatcher Powershell:通知 [英] LastWrite FileSystemWatcher Powershell: notification

查看:94
本文介绍了LastWrite FileSystemWatcher Powershell:通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当某个文件更改时,我希望收到通知.它工作正常,但我无法设置NotifyFilter. 未被识别为cmdlet的名称..." 主要问题是:我的脚本在我的函数中执行了两次.识别出更改,然后两次更改"

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
}

推荐答案

在代码中更改此内容以声明notifyfilter:

change this in your code for declaring notifyfilter:

$watcher.NotifyFilter = [System.IO.NotifyFilters]::LastWrite

但这不能解决问题,请阅读:

But this not solve the issue, read this:

对FileSystemWatcher组件进行故障排除" 部分...

From the "Troubleshooting FileSystemWatcher Components" section of the VS.NET documentation...

为单个操作生成的多个已创建事件 您可能会注意到,在某些情况下,单个创建事件会生成多个由组件处理的Created事件.例如,如果使用FileSystemWatcher组件监视目录中新文件的创建,然后使用记事本创建文件对其进行测试,则即使仅创建了一个文件,也可能会生成两个Created事件.这是因为记事本在写入过程中执行了多个文件系统操作.记事本分批写入磁盘,以创建文件内容,然后创建文件属性.其他应用程序可能以相同的方式执行.由于FileSystemWatcher监视操作系统的活动,因此将拾取这些应用程序触发的所有事件.

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.

注意:记事本可能还会引起其他有趣的事件生成.例如,如果您使用ChangeEventFilter指定仅希望监视属性更改,然后使用记事本将其写入正在监视的目录中的文件,则会引发一个事件.这是因为记事本在此操作过程中会更新文件的Archived属性.

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.

这篇关于LastWrite FileSystemWatcher Powershell:通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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