Free Information Technology Magazines and eBooks

Monday, May 04, 2009

.NET FileSystemWatcher, watch for changes in a specified directory

Ever wanted to be notified if there are file changes on a specified directory on your local computer, a network drive, or a remote computer? The watcher application should detect file event such as change, creation, deletion and rename. You can implement such kind of application in .NET using FileSystemWatcher Class. Using this class you can watch any activities users can do in your monitored folder.



To implement FileSystemWatcher Class, you need to include using System.IO namespace.

Here a sample folder monitoring C# code:

using System.IO;
// Watches the C:\Temp folder and notifies creation of new text files

public static void MonitorFolder(string strFolder)
{
// Create the FileSystemoFileSystemWatcher object and set its properties

FileSystemWatcher oFileWatcher = new FileSystemWatcher();
oFileWatcher.Path = @strFolder;
oFileWatcher.NotifyFilter = NotifyFilters.LastAccess
| NotifyFilters.LastWrite | NotifyFilters.FileName |
NotifyFilters.DirectoryName;
oFileWatcher.Filter = "*.txt";

// Add event handlers.

oFileWatcher.Created += new FileSystemEventHandler(OnChanged);
oFileWatcher.Changed += new FileSystemEventHandler(OnChanged);
oFileWatcher.Deleted += new FileSystemEventHandler(OnChanged);
oFileWatcher.Renamed += new RenamedEventHandler(OnRenamed);

// Begin watching/monitoring.

oFileWatcher.EnableRaisingEvents = true;

// Wait for the user to quit the program.

Console.WriteLine("Press \'x\' to exit the watcher.");
while(Console.Read()!='x');
}

// The event handlers

private static void OnChanged(object source, FileSystemEventArgs e)
{
Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType);
}
private static void OnRenamed(object source, RenamedEventArgs e)
{
// Specify what is done when a file is renamed.
Console.WriteLine("File: {0} renamed to {1}", e.OldFullPath, e.FullPath);
}


Lets translate it to VB.NET

Imports System.IO

Private Sub MonitorFolder(strFolder as String)
' Create a new FileSystemWatcher and set its properties.
Dim oFileWatcher As New FileSystemWatcher()
oFileWatcher.Path = strFolder
' Watch for changes in LastAccess and LastWrite times, and
' the renaming of files or directories.

oFileWatcher.NotifyFilter = (NotifyFilters.LastAccess Or NotifyFilters.LastWrite Or NotifyFilters.FileName Or NotifyFilters.DirectoryName)
' Only watch text files.
oFileWatcher.Filter = "*.txt"

' Add event handlers.
AddHandler oFileWatcher.Changed, AddressOf OnChanged
AddHandler oFileWatcher.Created, AddressOf OnChanged
AddHandler oFileWatcher.Deleted, AddressOf OnChanged
AddHandler oFileWatcher.Renamed, AddressOf OnRenamed

' Begin watching.
oFileWatcher.EnableRaisingEvents = True

' Wait for the user to exit the program.
Console.WriteLine("Press 'x' to exit the watcher.")
While Chr(Console.Read()) <> "x"
End While
End Sub

' Define the event handlers.
Private Shared Sub OnChanged(source As Object, e As FileSystemEventArgs)
' Specify what is done when a file is changed, created, or deleted.
Console.WriteLine("File: " & e.FullPath & " " & e.ChangeType)
End Sub

Private Shared Sub OnRenamed(source As Object, e As RenamedEventArgs)
' Specify what is done when a file is renamed.
Console.WriteLine("File: {0} renamed to {1}", e.OldFullPath, e.FullPath)
End Sub


Now add this function on your .NET application and begin monitoring your specified folder.

For more .NET tips and tricks, subscribe now

0 comments: