Click here to Skip to main content
13,557,068 members
Click here to Skip to main content
Add your own
alternative version


27 bookmarked
Posted 25 Nov 2002

Recursive directory watch

, 25 Nov 2002
Rate this:
Please Sign up or sign in to vote.
A simple filewatcher based using XML and treeview control
<!-- Download Links --> <!-- Article image -->

Sample Image - filesentry.jpg

<!-- Add the rest of your HTML here -->


FileSentry is a simple directory watcher. It will traverse a directory recursively, and build an XML document representing the directory structure. This XML document can be persisted to local storage and used for later comparison with the directory. Upon comparison, a TreeNodeView control will be populated and new items checked. This makes it easy to see which files have been added since the last time the XML was gotten.


I have my media library set up as a Linux Samba share and for some reason FileSystemWatcher events aren't triggered for these type of drives. I needed something that could quickly show any updates to the directory, obviously not having to browse through all the directories.

Using the code

There are two main methods indexDirectories and populateTree. The first method will recurse the given path and build and XML document like this, and store it in a private variable.


indexDirectories will call addDirectory which in turn will call addFile for each item in the directory if the item is a file, and addDirectory for each directory item.

// add directory to xml doc
private void indexDirectories(string path)
	DirectoryInfo dir = new DirectoryInfo(path); 
	XmlNode node = xmlDoc.CreateElement("directories");


// add file to xml directory
private void addFile(XmlNode xmlParent, DirectoryInfo dir)
	foreach(FileInfo theFile in dir.GetFiles("*.*"))
		XmlNode filmTitel = xmlDoc.CreateElement("filename");
		filmTitel.InnerText = theFile.Name; 

After that the populateTree method is called which takes an XML document as parameter, against which the recently built XML will be compared and populates the TreeView control. The populateTree method in turn will call addDirectoryTreeNode to help with the recursive populating of the TreeNodeView.

// add a new node for each directory to the treeview
private void addDirectoryTreeNode(XmlNode xmlNode, TreeNode parentNode)
	foreach(XmlNode xmlSubDir in xmlNode.SelectNodes("directory") )
		TreeNode parent = new TreeNode(xmlSubDir.SelectSingleNode("title").InnerText);
	foreach(XmlNode xmlSubFile in xmlNode.SelectNodes("filename"))
		TreeNode file = new TreeNode(xmlSubFile.InnerText);

The recursive calls are all pretty straight forward, and shouldn't cause any problems.

There are, as you can see, some parameters that are hard-coded which obviously shouldn't be the case in an "real" application. Bear with me on these as this is just a demo. Other quirks and peculiarities are all copyright of the author ;).


  • Released to  on 19 November, 2002.


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

Daniel Ripoll
Web Developer
Sweden Sweden
A seasoned COM/ATL developer, recently being hurled head first into his first .Net project, that loves scuba diving and music.

You may also be interested in...

Comments and Discussions

GeneralWhy the FileSystemWatcher doesn't work Pin
Heath Stewart12-Aug-03 7:01
editorHeath Stewart12-Aug-03 7:01 
QuestionBlurring? Pin
Phil Wright26-Nov-02 23:46
memberPhil Wright26-Nov-02 23:46 
AnswerRe: Blurring? Pin
danrip27-Nov-02 1:18
memberdanrip27-Nov-02 1:18 
GeneralRe: Blurring? Pin
Anthony_Yio2-Jun-03 17:16
memberAnthony_Yio2-Jun-03 17:16 
GeneralRe: Blurring? Pin
Igor Sukhov2-May-06 21:21
memberIgor Sukhov2-May-06 21:21 
Question"VC7"? Pin
Uwe Keim26-Nov-02 20:03
sitebuilderUwe Keim26-Nov-02 20:03 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.180515.1 | Last Updated 26 Nov 2002
Article Copyright 2002 by Daniel Ripoll
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid