473,396 Members | 1,666 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

File System Monitor Question

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
2 3291
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Paul | last post by:
Hi, VB.NET is saying the file I am creating is in use by another process and won't complete its task of moving the file to the specified destination folder. Here is my code (the main bit...
2
by: Henrik | last post by:
Hi Im trying to make an app that uses threading, I want to write to a file in a loop. How can i ensure that only one thread writes to the file a the time??? Im accessing the fil with: ...
4
by: vj | last post by:
What are the various tasks which can be done to avoid the error message " file System full" ? Thanks, Vj.
4
by: Peter Lin | last post by:
Dear all; I need to monitor a xml file so that I can update my data in hashtable, but the problem is it will trigger more than one event for a lastwrite action(I trigger this action by add...
5
by: ToddT | last post by:
i've got one app that writes large files to a specific directory that is watched by another app via an instance of the file system watcher class. my problem is that the second app is notified when...
0
by: Anurag | last post by:
Hi, ENV: DB2 ESE 8.2.3 DPF (11 nodes) on AIX 5.x ==== SCENARIO / SETUP ======== ====== (1) I needed to find out CPU Time (User / System) of SQL queries for benchmark testing. (2) I setup...
5
by: Tony Gravagno | last post by:
I have a class that instantiates two Timer objects that fire at different intervals. My class can be instantiated within a Windows Form or from a Windows Service. Actions performed by one of the...
4
by: mcterborg | last post by:
Hello everyone, here is what I want to do. I have a ATI video card and it allows me to setup profiles for monitor configurations and so forth. The shortcut to activate one of these profiles looks...
4
by: Uriel88 | last post by:
Hello, I am working with developing an application that uses the Netmon 3.2 API. Currently they have a PInvoke wrapper to access unmanaged C++ DLL functions. Basically what I am attempting to do...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.