Click here to Skip to main content
15,914,419 members
Articles / Programming Languages / C#
Article

File Processor

Rate me:
Please Sign up or sign in to vote.
3.70/5 (8 votes)
8 Jan 20041 min read 148.5K   618   44   40
An article on processing a File using the FileSystemWatcher class

Introduction

The purpose of this article is to address the lack of information that is available from the FileSystemWatcher utility class.

Background

The problem with the FileSystemWatcher class is we don't know when exactly has the file copy completed in order to process the file?

Using the code

The Monitor class wraps the FileSystemWatcher's functionality. The FileObject class encapsulates a newly copied file's general information as well as providing a method call that returns an output stream which can be manipulated.

C#
// Monitor Class
// This is the implementation of a FileSystemWatcher class.
// The static ConfigurationInfo class is a technique I use to 
// make global information available to an application.
// The Notifyfilters are important so that the required IO event is fired.
//
private void SystemWatch()
{        
    FileSystemWatcher watcher = new FileSystemWatcher();
    watcher.Path = ConfigurationInfo.ModulePath+
       ConfigurationInfo.MonitorPath;
    watcher.NotifyFilter = NotifyFilters.LastWrite  | 
       NotifyFilters.FileName ; 
    watcher.Filter = "*.*";
    watcher.Created += new FileSystemEventHandler(OnChanged);
    watcher.Changed += new FileSystemEventHandler(OnChanged);
    watcher.EnableRaisingEvents = true;
    while(!bStop)
    {
        oEvent.WaitOne(); 
    }
}

The next tricky bit is knowing when to start processing the file i.e. when has the file copy completed.

Someone else might come up with a better solution but in the RAD environment that I program in development speed and stability are very important. The trick is to user a SystemTimer which has a fairly low resource impact and increment the timer interval every time an IO event is fired. Obviously once the file copy has completed the interval will not be incremented and the timer will timeout.

C#
//
// The FileObject when constructed creates and starts the LogTimer class.
// LogTimer wraps a System timer.
//
private System.Timers.Timer LogTimer = new System.Timers.Timer();
.
.
//
// LogTimer constructor. The AutoReset set to false 
// means that the timer will only fire once.
//
public LogFileTimer(int tTime, string Name)
{
    PollTime = tTime;
    fileName = Name;
    LogTimer.Elapsed += new ElapsedEventHandler(Timeup);
    LogTimer.Interval = PollTime;
    LogTimer.Enabled = true;
    LogTimer.AutoReset = false; 
} 

Points of Interest

The big problem this solution solves is that large files copied into a monitored directory can only be processed once the copy or FTP has completed. I think the solution is neat but probably can be scaled down even further. I used these objects to convert a very large FTP'd flat file into XML.

Conclusion

This is my first article so go easy.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
South Africa South Africa
I studied IT in 1987 and have been working ever since in on projects that range from coding animation in pascal and assembler to writing secure internet banking web sites.

I live in South Africa and I think because of it's size we're required to do more for less which makes highly skilled in a wide range of skills and technologies.

I have the bonus of living in a coastal town so time away from computers is spent around the sea and in sunshine and nature.

Comments and Discussions

 
GeneralRe: Here's a solution... Pin
Almighty Bob23-Apr-04 20:42
Almighty Bob23-Apr-04 20:42 
isnt this the same as creating the created event???

if so, it still does not work because the last write is set multiple times during the creation of the file.

I had to create a similar class because I was also watching for a file to be created (the file was also coming from an ftp utility), and when it was done being created another app had to open it and use it. I ended up using the brute force technique mentioned below with a thread sleeping system put in place to keep the cycles down.

the thing with these events (changed, created, copied, deleted, ...) is that they fire at the beginning of the event not the end.

since with every write the lastxxx info is updated this would be thrown at every disk write (or something similar).

the way I tested my class to ensure that it worked properly was to generate a file (write 0's to a file) making it several (in my tests) gig in size. my file watcher class would watch for the creation of the file I would generate, and notify me when it was done. all attempts to use the built-in (notify filters, and so on) features of the class failed and I was able to determine the info I stated above.

the only solution I've found is similar to what has been shown here (except I decided against going the route he did...) I just created a new event, and used it in my app which watched the directory my ftp was dumping files into.

good article. with you'd have written it sooner (like this time last year hehe).

5of5

/bb|[^b]{2}/
GeneralRe: Here's a solution... Pin
cosmicb11-May-04 1:29
cosmicb11-May-04 1:29 
GeneralRe: Here's a solution... Pin
sides_dale10-Oct-08 19:01
sides_dale10-Oct-08 19:01 
QuestionHow to avoid polling while file is open Pin
Eron Wright29-Jan-04 12:18
Eron Wright29-Jan-04 12:18 
AnswerRe: How to avoid polling while file is open Pin
cosmicb18-Feb-04 0:54
cosmicb18-Feb-04 0:54 
GeneralRe: How to avoid polling while file is open Pin
Eron Wright18-Feb-04 3:56
Eron Wright18-Feb-04 3:56 
GeneralRe: How to avoid polling while file is open Pin
cosmicb27-Aug-04 6:03
cosmicb27-Aug-04 6:03 
GeneralRe: How to avoid polling while file is open Pin
Joerg Brunke17-Sep-04 8:05
Joerg Brunke17-Sep-04 8:05 
GeneralRe: How to avoid polling while file is open Pin
cosmicb21-Sep-04 1:37
cosmicb21-Sep-04 1:37 
QuestionHow do I configure application Pin
extremeg9-Jan-04 5:09
extremeg9-Jan-04 5:09 
AnswerRe: How do I configure application Pin
cosmicb12-Jan-04 1:07
cosmicb12-Jan-04 1:07 
GeneralInstead of counting I/O... Pin
Daniel Turini8-Jan-04 23:56
Daniel Turini8-Jan-04 23:56 
GeneralRe: Instead of counting I/O... Pin
cosmicb12-Jan-04 1:36
cosmicb12-Jan-04 1:36 
GeneralRe: Instead of counting I/O... Pin
Member 97736220-Jun-04 11:39
Member 97736220-Jun-04 11:39 
GeneralRe: Instead of counting I/O... Pin
cosmicb27-Aug-04 6:43
cosmicb27-Aug-04 6:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.