Saturday, July 2, 2011

Reacting To File System Events

In this blog entry I will tell some of the old tools I know to implement a program that reacts to Windows file system notifications.

What Is File System Event?

File system events occurs whenever a file or directory are created or modified in Windows or any other operating systems. You may want to monitor these events if your creating an IDE, or a text editor.

Using  System.IO.FileSystemWatcher in Visual Studio or Windows SDK

For me, using System.IO.FileSystemWatcher is the most easiest way to listen to file system change notifications using .NET. This component allows you to hook into Windows operating system and it notifies you whenever a file system event occur. A section on MSDN is available to guide you on how to create, configure and troubleshoot FileSystemWatcher component. See the MSDN File System Watcher guide. This component was available since .NET 1.1.

Using NIO.2 WatchService

Java does not contain any classes in the JDK to react or monitor file system events because file system events operations are platform specific and works different depending on the host operating system and file system type. Prior to Java 7 (which will be launch on July 7) the only pure Java way to achieve this is to create an infinite loop to poll the file system and look for updates. Another way is to use JNI (Java Native Interface) and code the implementation on C or C++.

The last way is to use the NIO.2 WatchService interface. Create a new WatchService instance and register watcher and create the event processing loop on the watcher. See File Change Notification in NIO.2.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.