Using the code below I am able to monitor a single directory for a new file
and then kick-off a process to deal with the file. The question is??? How
would I modify this code to be able to monitor a couple of different
directories and based upon the directory where the new file is created
kick-off a process
Example:
File A in Directory B starts process C
File F in Directory X starts process Y
Thanks
using System;
using System.IO;
using FileProcessor;
namespace DirectoryMonito rConsole
{
/// <summary>
/// Summary description for Class1.
/// </summary>
public class DirectoryMonito rConsole
{
private File_Processor objFileProcesso r;
/// <summary>
/// Used as a test application for the Directory Monitor Service
/// </summary>
[STAThread]
public static void Main()
{
// Define the directory to monitor
FileSystemWatch er watcher = new FileSystemWatch er();
watcher.Path = @"c:\FTPIN\PSIF L";
//watcher.Path = @"d:\FTPIN\PSIF L";
// Define what to monitor
watcher.NotifyF ilter = NotifyFilters.D irectoryName | NotifyFilters.F ileName;
// Define file filter
watcher.Filter = "*.*";// look for any new file
// Define event handlers
watcher.Created += new FileSystemEvent Handler(OnChang ed);
// Begin watching the directory
watcher.EnableR aisingEvents = true;
// Wait for the user to quit the program
Console.WriteLi ne(@"Press q to quit this program");
while(Console.R ead()!='q');
}
/// <summary>
/// Event handler for a new file put into the directory that is being
monitored
/// </summary>
/// <param name="source"></param>
/// <param name="e"></param>
private static void OnChanged(objec t source, FileSystemEvent Args e)
{
Console.WriteLi ne("File: {0} {1}!", e.FullPath, e.ChangeType);
// Get the name of the new file
// Make a reference to a directory
DirectoryInfo di = new DirectoryInfo(@ "c:\FTPIN\PSIFL ");
//DirectoryInfo di = new DirectoryInfo(@ "c:\FTPIN\PSIFL ");
// Get a reference for each file in the directory
FileInfo[] fi = di.GetFiles();
string strFileName = fi[0].ToString();
fi = null;
di = null;
File_Processor objFileProcesso r = new
FileProcessor.F ile_Processor(s trFileName);
objFileProcesso r = null;
}
}
}