Search notes:

System.IO.FileSystemWatcher (class)

System.IO.FileSystemWatcher monitors directories for changes of files and directories and notifies listeners of such changes.
The class inherits from System.ComponentModel.Component.
Possible types of events that can listed for are enumareted in System.IO.NotifyFilters. These events need to be combined with the enum System.IO.WatcherChangeType.

Watching a directory in PowerShell

function watch-directory {
   param (
      [string] $dir
   )

   $fsw = new-object System.IO.FileSystemWatcher  $dir

   $fsw.NotifyFilter          = [System.IO.NotifyFilters]::LastWrite
   $fsw.IncludeSubdirectories = $false

   while($true) {

    #
    # Watch directory for one second only to give user a chance
    # to break out of loop with ctrl-c.
    #
      $timeout = 1000

      [System.IO.WaitForChangedResult] $r = $fsw.WaitForChanged([System.IO.WatcherChangeTypes]::Changed, $timeout)

      if (-not $r.TimedOut) {
       #
       #  WaitForChanged returned because something changed in the
       #  watched directory, not because the timeout period
       #  was reached.
       #
         write-host ($r.Name + ": " + $r.ChangeType)
      }
   }
}
Github repository .NET-API, path: /System/IO/FileSystemWatcher/watch-directory.ps1

See also

Oracle's file watcher object.

Index