我有一个使用filesystemwatcher监视路径中文件夹的代码。
当用户在GUI中输入时,我需要它监视的路径基于两个字段中的输入进行更改。点击按钮后即可开始监视。
我确实有一个GUI可以像这样工作,它基于填写的这两个字段来启动.bat文件。 (实际上,我需要同时启动两个GUI,因为第一个打开文件并将文件保存到需要监视的文件夹中。)
我似乎无法让filesystemwatcher在同一单击上运行。
我尝试了各种方法将两者结合起来一起工作,但没有任何乐趣。 但是由于我对任何一种编码都不熟悉,所以我不知道自己在做错什么。
这是文件系统监视程序代码,当在文件夹中检测到文件时,该代码应运行.bat文件。 Folder1和Folder2需要能够根据GUI的输入进行更改。
$folder = 'c:\Work\Folder1\Folder2\Destination'
$filter = '*.*'
$fsw = New-Object IO.FileSystemWatcher $folder, $filter -Property @{IncludeSubdirectories = $true;NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'}
Register-ObjectEvent $fsw Created -SourceIdentifier FileCreated -Action {
$name = $Event.SourceEventArgs.Name
$changeType = $Event.SourceEventArgs.ChangeType
$timeStamp = $Event.TimeGenerated
Write-Host "The file '$name' was $changeType at $timeStamp" -fore green
Out-File -FilePath c:\temp\log\Filelog.txt -Append -InputObject "The file '$name' was $changeType at $timeStamp"
Set-Location "c:\Work\Folder1\Folder2\Destination"
Start-Process "c:\Work\Folder1\Folder2\Destination\PS_Run.bat"
}
下面是现有(Run.bat)代码在GUI中的工作方式
$TextBox.multiline = $false
$TextBox.location = New-Object System.Drawing.Point(50,50)
$TextBox.size = New-Object System.Drawing.Size(400,50)
$TextBox.Font = 'Microsoft Sans Serif,10'
$TextBox2 = New-Object system.Windows.Forms.TextBox
$TextBox2.multiline = $false
$TextBox2.location = New-Object System.Drawing.Point(50,110)
$TextBox2.size = New-Object System.Drawing.Size(400,50)
$TextBox2.Font = 'Microsoft Sans Serif,10'
\Folder1\Folder2\
$($TextBox.text)\$($TextBox2.text)
$TextBox
到$($TextBox.text)
在一种情况下有效,但在另一种情况下无效,我需要他们共同努力。
我得到的错误是
New-Object : Exception calling ".ctor" with "2" argument(s): "The directory name O:\EU_CGI_Resources\IMS\Automation_Tes
ting\Scripting\MaxScript\Texture_change_1\$($TextBox.text)\$($TextBox2.text)\Production\3D_Files\export is invalid."
At O:\EU_CGI_Resources\IMS\Automation_Testing\Scripting\Sctips_WIP\Watch_Path_Test.ps1:14 char:8
+ $fsw = New-Object IO.FileSystemWatcher $folder, $filter -Property @{I ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [New-Object], MethodInvocationException
+ FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand
Register-ObjectEvent : Cannot bind argument to parameter 'InputObject' because it is null.
At O:\EU_CGI_Resources\IMS\Automation_Testing\Scripting\Sctips_WIP\Watch_Path_Test.ps1:16 char:22
+ Register-ObjectEvent $fsw Created -SourceIdentifier FileCreated -Acti ...
+ ~~~~
+ CategoryInfo : InvalidData: (:) [Register-ObjectEvent], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.RegisterObj
ectEventCommand
Register-ObjectEvent : Cannot bind argument to parameter 'InputObject' because it is null.
At O:\EU_CGI_Resources\IMS\Automation_Testing\Scripting\Sctips_WIP\Watch_Path_Test.ps1:26 char:22
+ Register-ObjectEvent $fsw Deleted -SourceIdentifier FileDeleted -Acti ...
+ ~~~~
+ CategoryInfo : InvalidData: (:) [Register-ObjectEvent], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.RegisterObj
ectEventCommand