Click here to Skip to main content
6,631,889 members and growing! (17,029 online)
Email Password   helpLost your password?
Desktop Development » Files and Folders » Files     Intermediate

File Processor

By RichardRoe

An article on processing a File using the FileSystemWatcher class
C#, Windows, .NET, Visual Studio, Dev
Posted:8 Jan 2004
Views:93,788
Bookmarked:33 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
8 votes for this article.
Popularity: 3.27 Rating: 3.62 out of 5
1 vote, 12.5%
1
1 vote, 12.5%
2
1 vote, 12.5%
3
1 vote, 12.5%
4
4 votes, 50.0%
5

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.

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

About the Author

RichardRoe


Member
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.
Occupation: Web Developer
Location: South Africa South Africa

Other popular Files and Folders articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 40 (Total in Forum: 40) (Refresh)FirstPrevNext
GeneralSemaphore File Pinmembercosmicb15:42 6 Sep '07  
QuestionLower Case Full Path and Name PinmemberNizhum3:40 27 May '07  
GeneralCan you please tell how to test this program? PinmemberBharat Gadhia13:37 11 Aug '06  
GeneralRe: Can you please tell how to test this program? Pinmembercosmicb15:36 6 Sep '07  
GeneralCan you please tell how to test this program? PinmemberBharat Gadhia13:36 11 Aug '06  
GeneralCorrect Method For New Files Pinmemberrohdek16:56 22 Jul '06  
GeneralAnother approach without using timer or exception PinmemberChitrsen2:52 18 Jul '06  
QuestionRe: Another approach without using timer or exception Pinmembersides_dale17:48 14 Apr '07  
AnswerRe: Another approach without using timer or exception Pinmemberkrishpuma2:00 25 May '07  
GeneralHow to implement via windows services Pinmembervittoriod6:14 18 Oct '05  
GeneralBetter lightweight approach with FileShare exclusive open PinmemberPras Biswas6:34 10 Aug '05  
GeneralRe: Better lightweight approach with FileShare exclusive open Pinmemberrickyroe0:44 30 Jan '06  
GeneralWorks Great PinmemberNikhils336:44 16 May '05  
GeneralProgram bombs for large files PinmemberSimeon_Shapiro0:44 8 May '05  
GeneralRe: Program bombs for large files Pinmembersdelbos7:01 30 May '05  
GeneralRe: Program bombs for large files Pinmemberrickyroe0:46 30 Jan '06  
GeneralRe: Program bombs for large files Pinmemberrickyroe0:49 30 Jan '06  
GeneralRe: Program bombs for large files PinmemberThe Prisoner2:21 18 Sep '06  
Generalremove when event fires PinmemberTyler Jensen7:26 12 Apr '05  
GeneralRe: remove when event fires Pinmembercosmicb2:00 14 Apr '05  
GeneralWhy both onChanged and onCreated Pinmemberjabailo9:08 21 Mar '05  
GeneralRe: Why both onChanged and onCreated Pinmembercosmicb1:55 14 Apr '05  
Generaldoesn`t detect Copy Pinmemberkadnan23:36 27 Oct '04  
GeneralC#.net (saving a file ) PinsussAnonymous7:42 12 May '04  
GeneralHere's a solution... PinsussDamir Cuca16:41 23 Mar '04  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 8 Jan 2004
Editor: Nishant Sivakumar
Copyright 2004 by RichardRoe
Everything else Copyright © CodeProject, 1999-2009
Web21 | Advertise on the Code Project