Michael Wolf
{Binding ME}
Tuesday January 19, 2010
Silverlight 4 FileSystemWatcher ... no strings attached
In my last post I showed how to implement a bridge service using the new net.tcp binding in order to implement something not native to Silverlight 4, namely a FileSystemWatcher. While this works, as does Justin Angels com+ implementation (http://justinangel.net/#BlogPost=CuttingEdgeSilverlight4ComFeatures), it doesn't translate well to the mac, and eventually the mono , platform. However seeing Justin's com+ implementation inspired me to create a full Silverlight implementation using a background thread to look for changes. Overall this took around an hour.
In my ruff first pass implementation the file system watcher is a dependency object. Thus it can run its own background worker, and dispatch back to the UI thread to publish back events, without slowing down the ui thread looking for changes. It wakes up every 3 seconds to search the path looking for changed/deleted/ or additional files. With this "native Silverlight" implementation it is also restricted to the elevated trust System.IO file sandbox (folders: My Documents, My Pictures, My Videos , My Music and related subfolders see http://msdn.microsoft.com/en-us/library/ee721083(VS.96).aspx#file_system_access). Not as solid as having this functionality built directly into the framework, also does not give you the full os access like com and or a bridge service would. However it does give an option for building cross platform solutions which must monitor an accessible folder.
See the screencast below to see it in action.






