Setup a Hidden Camera for Watching a Folder (C#)

Watching activities inside a folder is often a task for application servers. For example, you have an application (APPLICATION1) which needs to wait until another application (APPLICATION2) uploads an XML file into a special folder. Once APPLICATION2 drops the file into the folder, APPLICATION2 does some processing according to the file contents.
 
using System.IO;
 
// Watches the C:\Temp folder and notifies creation of new text files
public static void WatchTempFolder()
{
    // Create the FileSystemoFileSystemWatcher object and set its properties
    FileSystemWatcher oFileSystemWatcher = new FileSystemWatcher();
    oFileSystemWatcher.Path = "C:\\Temp";
    oFileSystemWatcher.NotifyFilter = NotifyFilters.LastAccess
          | NotifyFilters.LastWrite | NotifyFilters.FileName |
            NotifyFilters.DirectoryName;
    oFileSystemWatcher.Filter = "*.txt";
    // Add event handlers.
    oFileSystemWatcher.Created += new FileSystemEventHandler(OnCreated);
    // Begin watching.
    oFileSystemWatcher.EnableRaisingEvents = true;
    // Wait for the user to quit the program.
    Console.WriteLine("Press \’q\’ to quit the sample.");
    while(Console.Read()!=’q’);
}
 
// The event handler
private static void OnCreated(object source, FileSystemEventArgs e)
{
    Console.WriteLine("File: " +  e.FullPath + " " + e.ChangeType);
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: