FileSystemWatcher 对 Excel 文件中的更改没有反应

FileSystemWatcher 对 Excel 文件中的更改没有反应

我正在尝试创建一个 systemFileWatcher,用于监视特定 excel 文件的任何更改。触发后,它应该运行一些命令,记录活动,然后继续监视。但是,目前我无法让监视程序对 .xlsx 文件中的更改做出反应。我的监视程序目前设置如下,基于在这里回答

$watch_path = 'D:\Projects\test\'
$watch_file = 'test.xlsx'

### SET FOLDER TO WATCH + FILES TO WATCH + SUBFOLDERS YES/NO
    $watcher = New-Object System.IO.FileSystemWatcher
    $watcher.Path = $watch_path
    $watcher.Filter = $watch_file
    $watcher.IncludeSubdirectories = $false
    $watcher.EnableRaisingEvents = $true  

### DEFINE ACTIONS AFTER AN EVENT IS DETECTED
    $action = { $path = $Event.SourceEventArgs.FullPath
                $changeType = $Event.SourceEventArgs.ChangeType
                $logline = "$(Get-Date), $changeType, $path"
                Add-content "log.txt" -value $logline
              }    
### DECIDE WHICH EVENTS SHOULD BE WATCHED 
    Register-ObjectEvent $watcher "Created" -Action $action
    Register-ObjectEvent $watcher "Changed" -Action $action
    while ($true) {sleep 60}

这不会在 Excel 程序中触发保存,但是当我将默认过滤器设置为“' 它对此保存所创建的临时文件做出反应。但我不希望这样,因为此文件夹中还有更多文件,观察器不应该对其做出反应。我该如何修复此过滤器/观察器?

编辑:我只是想为这个问题添加一些输出。添加行:Write-Host $logline创建 $logline 后,我得到以下输出:

Id     Name            PSJobTypeName   State         HasMoreData     Location             Command                  
--     ----            -------------   -----         -----------     --------             -------                  
1      7dd34e5a-484...                 NotStarted    False                                 $path = $Event.Source...
2      d7a74a71-5ae...                 NotStarted    False                                 $path = $Event.Source...

然后我打开 Excel 文件:

03/12/2024 11:34:39, Created, D:\Projects\test\~$test.xlsx
03/12/2024 11:34:40, Changed, D:\Projects\test\~$test.xlsx

然后我更改并保存 Excel 文件:

03/12/2024 11:35:06, Created, D:\Projects\test\845C7B30
03/12/2024 11:35:06, Changed, D:\Projects\test\845C7B30
03/12/2024 11:35:06, Changed, D:\Projects\test\845C7B30

最后我关闭了 Excel 文件,但没有收到任何进一步的消息。

答案1

由于 Excel 保存已注册为“重命名”事件,因此未触发该事件。因此,添加Register-ObjectEvent $watcher "Renamed" -Action $action包含其他操作寄存器的行会在 Excel 中保存时产生以下输出:

Id     Name            PSJobTypeName   State         HasMoreData     Location             Command                  
--     ----            -------------   -----         -----------     --------             -------                  
6      bc4f7a5b-747...                 NotStarted    False                                 $path = $Event.Source...
7      a3977062-fbb...                 NotStarted    False                                 $path = $Event.Source...
8      f28260d1-43a...                 NotStarted    False                                 $path = $Event.Source...
03/12/2024 11:50:04, Created, D:\Projects\test\3B0A8B30
03/12/2024 11:50:04, Created, D:\Projects\test\3B0A8B30
03/12/2024 11:50:04, Changed, D:\Projects\test\3B0A8B30
03/12/2024 11:50:04, Changed, D:\Projects\test\3B0A8B30
03/12/2024 11:50:04, Changed, D:\Projects\test\3B0A8B30
03/12/2024 11:50:05, Changed, D:\Projects\test\3B0A8B30
03/12/2024 11:50:05, Renamed, D:\Projects\test\69E4C836.tmp
03/12/2024 11:50:05, Renamed, D:\Projects\test\69E4C836.tmp
03/12/2024 11:50:05, Renamed, D:\Projects\test\test.xlsx
03/12/2024 11:50:06, Renamed, D:\Projects\test\test.xlsx

相关内容