I am using the following code to monitor a directory for new files. The
problem is that when I add a new file to the directory the code creates two
events??? what do I have to do to limit it to only one event??
using System;
using System.IO;
namespace DirectoryWatcher
{
/// <summary>
/// Summary description for Class1.
/// </summary>
///
class DirectoryWatcher
{
/// <summary>
/// The main entry point for the application.
/// </summary>
///
public static void Main()
{
// Establish which directory to watch
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = @"C:\FTPIN";
// Setup things to be on the lookout for
watcher.NotifyFilter = NotifyFilters.FileName;
// Setup filter for specific file types
watcher.Filter = "*.*";
// Define event handlers
watcher.Created += new FileSystemEventHandler(OnChanged);
// Begin watching the directory
watcher.EnableRaisingEvents = true;
// Wait for user to quit program
Console.WriteLine(@"Press q to quit");
while(Console.Read()!='q');
}
private static void OnChanged(object source, FileSystemEventArgs e)
{
Console.WriteLine("File: {0} {1}!", e.FullPath, e.ChangeType);
}
}
} |