FileSystemWatcher 不会因机器操作而触发

FileSystemWatcher 不会因机器操作而触发

我有这个脚本可以监视文件夹中的新文件:

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

如果我自己添加文件,它会很好地工作,但是当程序添加文件时它不会被触发。有什么想法为什么吗?

相关内容