PowerShell 文件目录监视片段

摘自《Windows PowerShell 2.0应用编程最佳实践》

#pseventing 1.1
#filesystemwatcher
Add-PSSnapin pseventing 

$fsw = New-Object system.IO.FileSystemWatcher
$fsw.Path = [IO.Path]::GetTempPath()
$fsw.EnableRaisingEvents = $true 

Start-KeyHandler -CaptureCtrlC 

Connect-Event fsw changed,deleted -Verbose 

"watching $($fsw.path)"
"entering loop ... ctrl+C to exit"
$done = $false
while($events = @(Read-Event -Wait)){
   #read-event alays returns a cllection
   foreach($event in $events){
      switch($event.name){
          "ctrlc"{
            "cancelling..."
            $done = $true
            }
        "changed"{
            $event
            #$done = $true
        }
        "deleted"{
            $event
        }
      }
    }
    if($done -eq $true){
        break
    }
}    
调试结果如下图:
image 

下面文章或许你还感兴趣

  • delphi下的FileSystemWatcher,实现文件目录监控
  • How to Enable AirPrint Service on Windows 7/Vista/XP (32-bit/64-bit)?
  • ScriptBlock执行的三种方法
  • PowerShell书籍收藏
  • PowerShell两本电子书籍
  • 收藏-Python调用windows下DLL详解
  • windows下利用OpenVPN搭建VPN服务器
  • 手把手教你配置Windows2003集群

Today on history:

  1. 2011:  How to Enable AirPrint Service on Windows 7/Vista/XP (32-bit/64-bit)?(0)

发表评论

电子邮件地址不会被公开。 必填项已用 * 标注

*

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>