473,605 Members | 2,411 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 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;

}

}

}
Nov 16 '05 #1
2 3303
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.co m

"Jack David" <JD****@NordisD irect.com> wrote in message
news:uZ******** *****@TK2MSFTNG P11.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 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;

}

}

}

Nov 16 '05 #2
There are two ways I can think of to solve this problem. One way would be
to create another FileSystemWatch er for the other directory. The other
option would be to have you FileSystemWatch er 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\subdir 1 do process A
else if new file created in c:\root1\subdir 2 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****@NordisD irect.Com>
From: "Jack David" <JD****@NordisD irect.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.publi c.dotnet.langua ges.csharp
NNTP-Posting-Host: 216.242.151.44
Path: cpmsftngxa10.ph x.gbl!TK2MSFTFE ED01.phx.gbl!TK 2MSFTNGP08.phx. gbl!TK2MSFTNGP1 1
phx.gblXref: cpmsftngxa10.ph x.gbl microsoft.publi c.dotnet.langua ges.csharp:2430 86
X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.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 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

FileSystemWatc her watcher = new FileSystemWatch er();

watcher.Path = @"c:\FTPIN\PSIF L";

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

// Define what to monitor

watcher.Notify Filter = NotifyFilters.D irectoryName | NotifyFilters.F ileName;
// Define file filter

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

// Define event handlers

watcher.Create d += new FileSystemEvent Handler(OnChang ed);

// Begin watching the directory

watcher.Enable RaisingEvents = true;

// Wait for the user to quit the program

Console.WriteL ine(@"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(objec t source, FileSystemEvent Args e)

{

Console.WriteL ine("File: {0} {1}!", e.FullPath, e.ChangeType);

// Get the name of the new file

// Make a reference to a directory

DirectoryInf o 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_Process or objFileProcesso r = new
FileProcessor. File_Processor( strFileName);

objFileProcess or = 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
715
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 anyway).... Private Sub LogChange(ByVal source As Object, ByVal e As System.IO.FileSystemEventArgs) If e.ChangeType = WatcherChangeTypes.Created Then
2
2840
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: Dim xFile As StreamWriter
4
2216
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
4009
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 something in the xml file using notepad). Here is my codes about using FilesystemWatcher fileWatcher=new FileSystemWatcher(); fileWatcher.Path=System.Environment.CurrentDirectory; fileWatcher.NotifyFilter=NotifyFilters.LastWrite;
5
2016
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 files are created, but the first app hasn't finished writing to it - causing a "file is already open" error when the second app trys to open the file for processing. what i need is a "file closed" event to be thrown by the file system watcher...
0
1807
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 an EVENT MONITOR WHERE AUTH_ID = 'ANURAG' (write to table).
5
12205
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 event handlers may take longer than the interval for either of the timers, so it's possible for multiple events to fire "simultaneously" and for events to queue up. I'm attempting to get the timers to sync on some reference type object, or use...
4
1869
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 like this: "C:\Program Files\ATI Technologies\ATI.ACE\CLI.exe" Load profilename="Just Monitor" Here is one example of how I've tried to do it:
4
4680
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 is rewrite an example application (written in C++ and provided in their documentation) in C#. Everything compiles fine and executes, but I have no .cap file at the end of the run. Some things that may be wrong: the ADAPTER_INDEX is not correct OR...
0
7999
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7931
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8411
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8281
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
5885
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3911
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2437
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1530
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1270
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.