FileSystemWatcher to execute .NET functions

后端 未结 1 1417
情歌与酒
情歌与酒 2020-12-22 01:09

I am creating a quick temporary fix so sorry if this is dirty, but-

Objective

I would like to use Powershells Register-Event cmd to wait fo

相关标签:
1条回答
  • 2020-12-22 01:45

    The following worked for me (code mostly copied from here):

    $folder = 'c:\Temp'
    $filter = '*.*'
    
    $monitor = New-Object IO.FileSystemWatcher $folder, $filter -Property @{
      IncludeSubdirectories = $false;
      NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'
    }
    
    Register-ObjectEvent $monitor Created -SourceIdentifier FileCreated -Action {
      $name = $Event.SourceEventArgs.FullPath
      $sr = New-Object System.IO.StreamReader($name)
      Write-Host $sr.ReadToEnd()
      $sr.Close()
    }
    
    0 讨论(0)
提交回复
热议问题