473,396 Members | 1,722 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.

filesystemwatcher process files on start

Hi

I have a problem with filesystemwatcher. My program works fine but there's no event to process existing files in the dir on startup and I'm not sure what is the correct way of doing this? I'm still learning c# so be gentle with me!
thanks
Aug 25 '08 #1
3 9782
tlhintoq
3,525 Expert 2GB
There isn't a built in function for that. What you need to do at the start of your process is raise the filesystemwatcher.created event for each file in the folder.

This will make it "look like" all the pre-existing files were just dropped into the folder.


Expand|Select|Wrap|Line Numbers
  1.                     foreach (string ThisFile in Directory.GetFiles(PhotoDirectoryPath,"*.jpg"))
  2.                     {
  3.                         string FilePath = Path.GetDirectoryName(ThisFile);
  4.                         string NameOnly = Path.GetFileName(ThisFile);
  5.                         FileSystemEventArgs FSEAe = new FileSystemEventArgs(WatcherChangeTypes.Created, FilePath, NameOnly);
  6.                         fileSystemWatcher1_Created(this, FSEAe);
  7.                     }
I'l warn you right now that trying to directly process from within your filesystemwater.created method can be risky. Depending on how much proccessing you are doing and how many files already exist in the directory you can quickly create a crash.

What happens if there are 5,000 files in the directory when you hit [Start]? You will create 5,000 instances of the .created method.

I suggest that you use the .created method only for adding the new files to a list to be processed, then process that list periodically. This works for me.
Aug 25 '08 #2
Also there's always:

Expand|Select|Wrap|Line Numbers
  1.         private FileSystemWatcher fileSystemWatcher;
  2.         private void Form1_Load(object sender, EventArgs e)
  3.         {
  4.             String[] filesPresent = Directory.GetFiles(@"C:\");
  5.             fileSystemWatcher = new FileSystemWatcher(@"C:\");
  6.             fileSystemWatcher.Created += new FileSystemEventHandler(fileSystemWatcher_Created);
  7.             fileSystemWatcher.EnableRaisingEvents = true;
  8.  
  9.             foreach (String filename in filesPresent)
  10.                 RealWork(filename);
  11.         }
  12.  
  13.         void fileSystemWatcher_Created(object sender, FileSystemEventArgs e)
  14.         {
  15.             RealWork(e.FullPath);
  16.         }
  17.  
  18.         private void RealWork(String filename)
  19.         {
  20.             MessageBox.Show(filename);
  21.             // ...
  22.         }
Aug 25 '08 #3
tlhintoq
3,525 Expert 2GB
Also there's always:

Expand|Select|Wrap|Line Numbers
  1.         private FileSystemWatcher fileSystemWatcher;
  2.         private void Form1_Load(object sender, EventArgs e)
  3.         {
  4.             String[] filesPresent = Directory.GetFiles(@"C:\");
  5.             fileSystemWatcher = new FileSystemWatcher(@"C:\");
  6.             fileSystemWatcher.Created += new FileSystemEventHandler(fileSystemWatcher_Created);
  7.             fileSystemWatcher.EnableRaisingEvents = true;
  8.  
  9.             foreach (String filename in filesPresent)
  10.                 RealWork(filename);
  11.         }
  12.  
  13.         void fileSystemWatcher_Created(object sender, FileSystemEventArgs e)
  14.         {
  15.             RealWork(e.FullPath);
  16.         }
  17.  
  18.         private void RealWork(String filename)
  19.         {
  20.             MessageBox.Show(filename);
  21.             // ...
  22.         }
DonBytes, In essense your approach and mine are the same give or take a little personal coding style: Get the list of files and raise a Created event for it. But as I mentioned, if you try handling all the work directly as you have here in the RealWork() method when there are 5000 files in the directory you're going to crash. There just isn't enough horsepower to go around to handle all that work in the average or even above-average box. It works fine for a simple coding example, but measures have to be taken for extreme situations, like creating a list of files to process then spawn only as many threads as can your box can afford, or handle them sequentially in a loop or whatever makes sense for the particular program.
Aug 26 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Josh Usovsky | last post by:
I'm setting up a watched folder using FileSystemWatcher. When I drop a small file into the watched folder, I can respond to a .Created event and process the file with other code. However, if I try...
2
by: Charlie Kunkel | last post by:
I need help. I have a directory I'm watching for creation of .TIF files, whereupon creation, I need to launch a process (command line exe) that converts the TIF file to a postscript file. (using...
1
by: Troy Murphy | last post by:
How do I prevent the FileSystemWatcher event to keep firing while the file is being created? When copying a file to the watched folder, the event fires a dozen or more times! Also, the...
3
by: Craig Thompson | last post by:
I've attempted to write a windows service that creates one FileSystemWatcher for each entry in a XML config file. Everything start perfrectly and runs as I expect for about 5 minutes and then...
1
by: Primo | last post by:
Hello, I am building a data management application with the following processes: Process 1 is a Windows service which uses FileSystemWatcher to monitor a directory. Process 2 opens a file...
1
by: Phil396 | last post by:
I have a windows service that uses a filesystemwatcher to wait for files and process them to a database. Sometimes a large group of files will be cut and paste for the filesystemwatcher to...
3
by: Stampede | last post by:
Hi, I write an application which waits for incomming files in a specified directory. I thought, that using the FileSystemWatcher would be the best, as it does exactly what I need. But now I have...
2
by: Nick K. | last post by:
I'm trying to implement a FileSystemWatcher from a Windows Service. Everything works until I: Stop the Service Copy a file to the watched directory. Re-Start the Service. I would expect the...
2
by: Sacha Korell | last post by:
I need to set up a FileSystemWatcher in my web application (to automatically process uploaded files) and I'm trying to decide where to set it up. I would like to keep it within the web app, but it...
4
by: Stefan L | last post by:
Hi NG, I have a file driven application (a report server) which has to do some work when new files arrive or are deleted. When processing the notifications about newly created files from a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
0
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...
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
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.