Click here to Skip to main content
Licence 
First Posted 8 Jan 2004
Views 108,027
Bookmarked 41 times

File Processor

By | 8 Jan 2004 | Article
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.

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

Web Developer

South Africa South Africa

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.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralSemaphore File Pinmembercosmicb14:42 6 Sep '07  
QuestionLower Case Full Path and Name PinmemberNizhum2:40 27 May '07  
QuestionCan you please tell how to test this program? PinmemberBharat Gadhia12:37 11 Aug '06  
AnswerRe: Can you please tell how to test this program? Pinmembercosmicb14:36 6 Sep '07  
QuestionCan you please tell how to test this program? PinmemberBharat Gadhia12:36 11 Aug '06  
GeneralCorrect Method For New Files Pinmemberrohdek15:56 22 Jul '06  
GeneralAnother approach without using timer or exception PinmemberChitrsen1:52 18 Jul '06  
QuestionRe: Another approach without using timer or exception Pinmembersides_dale16:48 14 Apr '07  
AnswerRe: Another approach without using timer or exception Pinmemberkrishpuma1:00 25 May '07  
QuestionHow to implement via windows services Pinmembervittoriod5:14 18 Oct '05  
GeneralBetter lightweight approach with FileShare exclusive open PinmemberPras Biswas5:34 10 Aug '05  
This lightweight approach works great:
http://www.issociate.de/board/post/234329/FileSystemWatcher_question.html
 

Private Sub CallingProcess
System.Threading.Thread.Sleep(100)
 
''try to open the file in exclusive mode
Do Until TryToOpenInExclusiveMode(e.Name) = True
System.Threading.Thread.Sleep(1000)
Loop
 
End Sub
 

Private Function TryToOpenInExclusiveMode(ByVal xmlFileName As String) As Boolean
Dim fs As FileStream
Try
fs = New FileStream((archiveFileDirectory & "\" & xmlFileName), FileMode.OpenOrCreate, FileAccess.Read, FileShare.None)
fs.Close()
fs = Nothing
Return True
Catch ioEx As IOException
fs.Close()
fs = Nothing
Return False
Catch fnfEx As FileNotFoundException
fs.Close()
fs = Nothing
Return False
Catch uaEx As UnauthorizedAccessException
fs.Close()
fs = Nothing
Return False
End Try
End Function
 
Pras Biswas
GeneralRe: Better lightweight approach with FileShare exclusive open Pinmemberrickyroe23:44 29 Jan '06  
GeneralWorks Great PinmemberNikhils335:44 16 May '05  
GeneralProgram bombs for large files PinmemberSimeon_Shapiro23:44 7 May '05  
GeneralRe: Program bombs for large files Pinmembersdelbos6:01 30 May '05  
GeneralRe: Program bombs for large files Pinmemberrickyroe23:46 29 Jan '06  
GeneralRe: Program bombs for large files Pinmemberrickyroe23:49 29 Jan '06  
GeneralRe: Program bombs for large files PinmemberThe Prisoner1:21 18 Sep '06  
Generalremove when event fires PinmemberTyler Jensen6:26 12 Apr '05  
GeneralRe: remove when event fires Pinmembercosmicb1:00 14 Apr '05  
GeneralWhy both onChanged and onCreated Pinmemberjabailo8:08 21 Mar '05  
GeneralRe: Why both onChanged and onCreated Pinmembercosmicb0:55 14 Apr '05  
Generaldoesn`t detect Copy Pinmemberkadnan22:36 27 Oct '04  
GeneralC#.net (saving a file ) PinsussAnonymous6:42 12 May '04  
GeneralHere's a solution... PinsussDamir Cuca15:41 23 Mar '04  

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

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

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120528.1 | Last Updated 9 Jan 2004
Article Copyright 2004 by RichardRoe
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid