65.9K
CodeProject is changing. Read more.
Home

File Processor

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.70/5 (8 votes)

Jan 9, 2004

1 min read

viewsIcon

151062

downloadIcon

620

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.

// 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.

//
// 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.