5,667,575 members and growing! (15,803 online)
Email Password   helpLost your password?
Languages » XML » XML/XSLT     Intermediate

Recursive directory watch

By Daniel Ripoll

A simple filewatcher based using XML and treeview control
C#, VC7, C++, Windows, .NET, .NET 1.0VS.NET2002, Visual Studio, Dev

Posted: 25 Nov 2002
Updated: 25 Nov 2002
Views: 62,252
Bookmarked: 22 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
11 votes for this Article.
Popularity: 3.73 Rating: 3.58 out of 5
2 votes, 18.2%
1
2 votes, 18.2%
2
0 votes, 0.0%
3
3 votes, 27.3%
4
4 votes, 36.4%
5

Sample Image - filesentry.jpg

Introduction

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.

Background

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.

			<directories>
				<directory>
		 			<title>documents</title>
					<filename>123.txt</filename>
					<filename>456.txt</filename>
					<filename>789.txt</filename>
					<filename>abc.txt</filename>
				</directory>
			</directories>
			

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");
	if(xmlDoc.HasChildNodes)
		xmlDoc.ReplaceChild(node,xmlDoc.FirstChild); 
	else
		xmlDoc.AppendChild(node);

	addDirectory(xmlDoc.FirstChild,dir); 	

}
// 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; 
		if(xmlParent.HasChildNodes)
			xmlParent.InsertBefore(filmTitel,null); 
		else
			xmlParent.AppendChild(filmTitel); 
	}
}		

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);
		parentNode.Nodes.Add(parent);
		addDirectoryTreeNode(xmlSubDir,parent);
	}
	
	foreach(XmlNode xmlSubFile in xmlNode.SelectNodes("filename"))
	{
		TreeNode file = new TreeNode(xmlSubFile.InnerText);
		parentNode.Nodes.Add(file);
	}
				
}
		

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 ;).

History

  • Released to codeproject.com  on 19 November, 2002.

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

Daniel Ripoll


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

Other popular XML articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 6 of 6 (Total in Forum: 6) (Refresh)FirstPrevNext
GeneralWhy the FileSystemWatcher doesn't workeditorHeath Stewart8:01 12 Aug '03  
GeneralBlurring?memberPhil Wright0:46 27 Nov '02  
GeneralRe: Blurring?memberdanrip2:18 27 Nov '02  
GeneralRe: Blurring?memberAnthony_Yio18:16 2 Jun '03  
GeneralRe: Blurring?memberIgor Sukhov22:21 2 May '06  
General"VC7"?sitebuilderUwe Keim21:03 26 Nov '02  

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

PermaLink | Privacy | Terms of Use
Last Updated: 25 Nov 2002
Editor: Barry Lapthorn
Copyright 2002 by Daniel Ripoll
Everything else Copyright © CodeProject, 1999-2008
Web12 | Advertise on the Code Project