By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,264 Members | 1,893 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,264 IT Pros & Developers. It's quick & easy.

File System Monitor Question

P: n/a
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 DirectoryMonitorConsole

{

/// <summary>

/// Summary description for Class1.

/// </summary>

public class DirectoryMonitorConsole

{

private File_Processor objFileProcessor;

/// <summary>

/// Used as a test application for the Directory Monitor Service

/// </summary>

[STAThread]

public static void Main()

{

// Define the directory to monitor

FileSystemWatcher watcher = new FileSystemWatcher();

watcher.Path = @"c:\FTPIN\PSIFL";

//watcher.Path = @"d:\FTPIN\PSIFL";

// Define what to monitor

watcher.NotifyFilter = NotifyFilters.DirectoryName | NotifyFilters.FileName;

// Define file filter

watcher.Filter = "*.*";// look for any new file

// Define event handlers

watcher.Created += new FileSystemEventHandler(OnChanged);

// Begin watching the directory

watcher.EnableRaisingEvents = true;

// Wait for the user to quit the program

Console.WriteLine(@"Press q to quit this program");

while(Console.Read()!='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(object source, FileSystemEventArgs e)

{

Console.WriteLine("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 objFileProcessor = new
FileProcessor.File_Processor(strFileName);

objFileProcessor = null;

}

}

}
Nov 16 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Jack,

I would just take a command-line argument specifying the directory to
watch. That way, when you kick off the process, you can pass in the new
directory that that process should watch.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Jack David" <JD****@NordisDirect.com> wrote in message
news:uZ*************@TK2MSFTNGP11.phx.gbl...
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 DirectoryMonitorConsole

{

/// <summary>

/// Summary description for Class1.

/// </summary>

public class DirectoryMonitorConsole

{

private File_Processor objFileProcessor;

/// <summary>

/// Used as a test application for the Directory Monitor Service

/// </summary>

[STAThread]

public static void Main()

{

// Define the directory to monitor

FileSystemWatcher watcher = new FileSystemWatcher();

watcher.Path = @"c:\FTPIN\PSIFL";

//watcher.Path = @"d:\FTPIN\PSIFL";

// Define what to monitor

watcher.NotifyFilter = NotifyFilters.DirectoryName | NotifyFilters.FileName;
// Define file filter

watcher.Filter = "*.*";// look for any new file

// Define event handlers

watcher.Created += new FileSystemEventHandler(OnChanged);

// Begin watching the directory

watcher.EnableRaisingEvents = true;

// Wait for the user to quit the program

Console.WriteLine(@"Press q to quit this program");

while(Console.Read()!='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(object source, FileSystemEventArgs e)

{

Console.WriteLine("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 objFileProcessor = new
FileProcessor.File_Processor(strFileName);

objFileProcessor = null;

}

}

}

Nov 16 '05 #2

P: n/a
There are two ways I can think of to solve this problem. One way would be
to create another FileSystemWatcher for the other directory. The other
option would be to have you FileSystemWatcher monitor a directory higher up
in the hierarchy and then start the appropriate process based on where the
file was created. Something like this (pseudocode):

Monitor c:\root1
if new file created in c:\root1\subdir1 do process A
else if new file created in c:\root1\subdir2 do process B
else ...

Of course, this watches all files created in c:\root1, so depending on how
much activity that directory gets this might be more expensive than
watching the two specific directories. This also assumes that the
directories to watch are located in a common directory. One could watch
the root drive, but I think that would be way too expensive, but I don't
have any data to back that up.

hth

-Joel
--------------------
Reply-To: "Jack David" <JD****@NordisDirect.Com>
From: "Jack David" <JD****@NordisDirect.com>
Subject: File System Monitor Question
Date: Mon, 10 May 2004 11:25:29 -0400
Lines: 138
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
Message-ID: <uZ*************@TK2MSFTNGP11.phx.gbl>
Newsgroups: microsoft.public.dotnet.languages.csharp
NNTP-Posting-Host: 216.242.151.44
Path: cpmsftngxa10.phx.gbl!TK2MSFTFEED01.phx.gbl!TK2MSFT NGP08.phx.gbl!TK2MSFTNGP11
phx.gblXref: cpmsftngxa10.phx.gbl microsoft.public.dotnet.languages.csharp:243086
X-Tomcat-NG: microsoft.public.dotnet.languages.csharp

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 DirectoryMonitorConsole

{

/// <summary>

/// Summary description for Class1.

/// </summary>

public class DirectoryMonitorConsole

{

private File_Processor objFileProcessor;

/// <summary>

/// Used as a test application for the Directory Monitor Service

/// </summary>

[STAThread]

public static void Main()

{

// Define the directory to monitor

FileSystemWatcher watcher = new FileSystemWatcher();

watcher.Path = @"c:\FTPIN\PSIFL";

//watcher.Path = @"d:\FTPIN\PSIFL";

// Define what to monitor

watcher.NotifyFilter = NotifyFilters.DirectoryName | NotifyFilters.FileName;
// Define file filter

watcher.Filter = "*.*";// look for any new file

// Define event handlers

watcher.Created += new FileSystemEventHandler(OnChanged);

// Begin watching the directory

watcher.EnableRaisingEvents = true;

// Wait for the user to quit the program

Console.WriteLine(@"Press q to quit this program");

while(Console.Read()!='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(object source, FileSystemEventArgs e)

{

Console.WriteLine("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 objFileProcessor = new
FileProcessor.File_Processor(strFileName);

objFileProcessor = null;

}

}

}

--------------------------------------------------------------------
This reply is provided AS IS, without warranty (express or implied).
Nov 16 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.