|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionFileSentry 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. BackgroundI 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 codeThere are two main methods <directories>
<directory>
<title>documents</title>
<filename>123.txt</filename>
<filename>456.txt</filename>
<filename>789.txt</filename>
<filename>abc.txt</filename>
</directory>
</directories>
// 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 // 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
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||