![FileSystemWatcher 不会因机器操作而触发](https://linux22.com/image/1556127/FileSystemWatcher%20%E4%B8%8D%E4%BC%9A%E5%9B%A0%E6%9C%BA%E5%99%A8%E6%93%8D%E4%BD%9C%E8%80%8C%E8%A7%A6%E5%8F%91.png)
我有这个脚本可以监视文件夹中的新文件:
$watcher = New-Object System.IO.FileSystemWatcher
$watcher.Path = "C:\source"
$watcher.Filter = "*.xml*"
$watcher.IncludeSubdirectories = $true
$watcher.EnableRaisingEvents = $true
$action = { $path = $Event.SourceEventArgs.FullPath
$xml = [xml](get-content $Path)
$checkpoint = $xml.recognitionevent.checkpoint
$licenseplate = $xml.recognitionevent.frontlicenseplates.licenseplate.formatted
If ($licenseplate -eq 9224026)
{
If ($checkpoint -eq 1)
{
$write = "7"
} ElseIf ($checkpoint -eq 2 -or 3)
{
$write = "0"
}
$com = "com5"
$baud = "9600"
$port = $port= new-Object System.IO.Ports.SerialPort $com,$baud,None,8,one
$port.open()
$port.write($write)
$port.close()
}
$logline = "$licenseplate, $checkpoint"
Add-content "C:\Users\bill\Documents\Script\log.txt" -Value $logline
}
Register-ObjectEvent $watcher "Created" -Action $action
while ($true) {sleep 1}
如果我自己添加文件,它会很好地工作,但是当程序添加文件时它不会被触发。有什么想法为什么吗?