Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Powershell-全局注册IO.FileSystemWatcher_C#_Powershell_Events_Filesystems_Global - Fatal编程技术网

C# Powershell-全局注册IO.FileSystemWatcher

C# Powershell-全局注册IO.FileSystemWatcher,c#,powershell,events,filesystems,global,C#,Powershell,Events,Filesystems,Global,我已经成功创建了一个FileSystemWatcher(C#对象)。我在powershell会话中运行以下代码 # Filters $filter = "somefile.txt" $flagfolder = "C:\path\to\some\folder" # Instantiate Watcher $Watcher = New-Object IO.FileSystemWatcher $flagfolder, $filter -Property @{ IncludeSubdire

我已经成功创建了一个FileSystemWatcher(C#对象)。我在powershell会话中运行以下代码

# Filters
$filter = "somefile.txt"
$flagfolder = "C:\path\to\some\folder"

# Instantiate Watcher 
$Watcher = New-Object IO.FileSystemWatcher $flagfolder, $filter -Property @{ 
    IncludeSubdirectories = $false
    NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'
}

# EVENT: $filter is created
$onCreated = Register-ObjectEvent $Watcher Created -SourceIdentifier FileCreated -Action {
   $path = $Event.SourceEventArgs.FullPath
   $name = $Event.SourceEventArgs.Name
   $changeType = $Event.SourceEventArgs.ChangeType
   $timeStamp = $Event.TimeGenerated
   Write-Host "The file '$name' was $changeType at $timeStamp"
   Write-Host $path
   someglobalfunction $param
}
运行上述代码后,如果执行
Get作业
,它会报告FileWatcher:

Id     Name            PSJobTypeName   State         HasMoreData     Location  
--     ----            -------------   -----         -----------     --------  
74     FileCreated                     NotStarted    False  
但是,如果我打开一个新的powershell会话并执行一个
获取作业
它不会报告任何内容


每当任何人任何人创建文件
$pathfolder\somefile.txt
。。。但目前,它仅在定义
$watcher
的会话创建文件时起作用。

要实现这一点,需要两件事

  • newobjectsystem.IO.FileSystemWatcher
  • 注册ObjectEvent
  • 我认为这是使FileSystemWatcher工作所需的最低代码

    设置FileSystemWatcher对象

    $w = New-Object System.IO.FileSystemWatcher
    $w.Path = $PWD
    
    $PWD
    (当前工作目录)是一个自动变量

    订阅“已创建”事件(为简单起见)

    现在,如果在当前工作目录中创建文件,PowerShell将在屏幕上写入“已创建文件”。在当前会话中,在不同的PowerShell会话中,通过文件资源管理器,使用cmd.exe,如何创建该文件并不重要


    有更复杂的代码。但是这里的简化代码是直接基于它的。

    作业不像你想象的那样工作。请尝试以下代码。“Get-Job cmdlet获取表示在当前会话中启动的后台作业的对象。”添加了强调。另外,您为
    $flag
    分配了一个值,但从不使用
    $flag
    @MikeSherrill'CatRecall'对不起,我指的是$filter。所以我认为这是看文件的错误方式?
    Register-ObjectEvent -InputObject $w -EventName Created -SourceIdentifier "File.Created" `
        -Action {Write-Host -Object "A file was created" -ForegroundColor Green -BackgroundColor Black}