/\/\o\/\/ PowerShelled

This blog has moved to http://ThePowerShellGuy.com Greetings /\/\o\/\/
$AtomFeed = ("Atom.xml")
$PreviousItems = (" StartRDP script "," convert images in MSH "," Style Problems "," ColorCoding MSH scripts on blog "," GUI folder copy tool "," MSHObjectViewer mentioned in More talking about Mo... "," MSH registry access "," Unix => MSH Translations: find and grep "," MSH Object Viewer "," Windows "Monad" Shell Beta 2 Documentation Pack "," ")

Friday, October 21, 2005

 


MSH directory watcher with popup-balloon



there are some troubles with using the Filewatcher class for monitoring file changes.
in MSH.

Main problem is that the filewatcher is on another Thread, hence you can't (yet) use a delegate .



"Jeff Jones [MSFT]" wrote:
This is a known issue where if the
ScriptBlock gets called on a different thread then it cannot be invoked.
It looks like the plan for V1 is to improve the error message and then possibly
take a fix in V2.


you still can do this :




$fsw = New-object System.IO.FileSystemWatcher
$r = new-object System.IO.WaitForChangedResult
$r = $fsw.WaitForChanged($all)


but then you can't quit no more (only with ctrl-break but that will exit MSH)a workaround a made for this was :


while ($True) {
write-host -noNewLine "."
$fsw = New-object System.IO.FileSystemWatcher
$r = new-object System.IO.WaitForChangedResult
$fsw.path = $args[0]
$r = $fsw.WaitForChanged($all,10000)
if ($r.timedOut -eq $False){
$changed = $True
$r fl out-string
}
}

so you can quit every 10 seconds.
But the looping is not verry elegant, soI did not really like this

The trick with the solution below is to use a form for the Filesystemwatcher to sync with.( a form has an interface for that)

I have got a lot of help with this issue in the newsgroup from Bruce Payette [MSFT]
he made a nice script to make a form with a quit button(some nice lay-out tricks also)

but in this case I didn't want to see the form.
so I made this version that uses a notify-icon,so now you can quit from the notify-icon, hence I was able to hide the form.

nice site-effect of Using the Notify-Icon for this was that I could Use Balloons to.
And I have a Context-menu, so I can add More functions (to add an other watcher / change the dir etc.)

(*TIP if you have the MSHObjectViewer function, from other post. you could do OV($FSW) here ;-)

So I think It's a nice base for allot of functions.

enjoy ...

gr /\/\o\/\/


#WatchDir.msh
#
# Watches Directory for changes,
# and you can Quit !!
# (and a balloon as a bonus ;-)
#
# /\/\o\/\/ 2005
# http://mow001.blogspot.com

[System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms")


function watch {
      $dir = $args[0]
      if ($dir -eq $null){$dir=$((gi .).Fullname)}
      if ($dir -eq "."){$dir=$((gi .).Fullname)}
      if ([system.io.directory]::exists($dir)) {

        "watching $dir ... Use systray to quit"

        $form = new-object System.Windows.Forms.form
        $cm = new-object System.Windows.Forms.ContextMenu
        $mi = new-object System.Windows.Forms.MenuItem
        $ni = new-object System.Windows.Forms.NotifyIcon

        $form.contextMenu = $cm
        $form.contextMenu.MenuItems.AddRange($mi)
        $form.ShowInTaskbar = $False
        $form.WindowState = "minimized"  

        $mi.Index = 0
        $mi.Text = "E&xit"
        $mi.add_Click({
          #Clean up
          $fsw.EnableRaisingEvents = $False;$NI.Visible = $False
          $form.close()
        })


        $ni = $ni
        $ni.Icon = New-object System.Drawing.Icon("g:\monad\mow.ico")
        $NI.ContextMenu = $cm
        $NI.Text = "Watching $dir"

        $fsw = new-object System.IO.FileSystemWatcher $dir
        $fsw.IncludeSubDirectories = $true
        $fsw.SynchronizingObject = $form
        $fsw.add_Changed({
          $NI.ShowBalloonTip(10,$_.FullPath,"$($_.FullPath)$($_.ChangeType)",[system.windows.forms.ToolTipIcon]"Warning")
          write-host "$((get-date).ToShortTimeString()) : $($_.FullPath)$($_.ChangeType)"
        })

        #Fire it up
        $fsw.EnableRaisingEvents = $true;$NI.Visible = $True;$form.showdialog()
  }
  else {"Directory $dir does not exist"}

}



Comments:
Blogger /\/\o\/\/
also nice one for adding to the watcher, will also exit the shell if used from MSH directly

$mycomputer.network.add_NetworkAvailabilityChanged({"Boem"})

have fun

gr /\/\o\/\/
 
Blogger DontBotherMeWithSpam
I think this has been the most fun script i have seen so far :) Enjoying it so much~
 
Post a Comment

Links to this post:

Create a Link



<< Home

Archives

October 2005   November 2005   December 2005   January 2006   February 2006   March 2006   April 2006   May 2006   June 2006   July 2006   August 2006   September 2006   October 2006   November 2006   December 2006  

$Links = ("PowerShell RC1 Docs"," PowerShell RC1 X86"," PowerShell RC1 X64"," Monad GettingStarted guide"," Monad Progamming Guide"," Monad SDK"," Monad videos on Channel 9"," MSH Community Workspace"," scripts.readify.net "," MonadSource"," www.reskit.net"," PowerShell Blog"," Under The Stairs"," computerperformance powershell Home"," proudlyserving"," MSH on wikipedia"," MSHWiki Channel 9"," Keith Hill's Blog"," Precision Computing"," PowerShell for fun"," MSH Memo (Japanese)"," monadblog")

find-blog -about "PowerShell","Monad" | out-Technorati.
find-blog -contains "","" | out-Technorati.
Google
 
Web mow001.blogspot.com

This page is powered by Blogger. Isn't yours?