Click here to Skip to main content
15,897,315 members
Articles / Programming Languages / C#

Generic Tree Control

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
8 Feb 2009CPOL2 min read 25K   184   16  
An article on a generic tree collection
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

using Precio.Collections;


namespace Precio.IO.Collection
{
    /// <summary>
    /// 
    /// </summary>
    /// <author>Martin Dahlstedt 2006 (m_dahlstedt@hotmail.com)</author>
    [Serializable()]
    public sealed class FileSystemTree : Tree<FileSystemNodeValue>
    {
        private DirectoryInfo _rootFolder;
                        

        /// <summary>
        /// Constructor for FileSystemTree.
        /// </summary>
        /// <param name="rootFolder">Path to the root folder.</param>
        public FileSystemTree(DirectoryInfo rootFolder)
            : base(new FileSystemNodeValue(rootFolder))
        {
            _rootFolder = rootFolder;
        }


        /// <summary>
        /// 
        /// </summary>
        protected override void BuildIndexInternal()
        {
            this.BuildIndex(_rootFolder, base.RootNode);
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="directory"></param>
        /// <param name="walkingTreeNode"></param>
        private void BuildIndex(DirectoryInfo directory, TreeNode<FileSystemNodeValue> walkingTreeNode)
        {
            String relativePath = directory.FullName.Substring(_rootFolder.FullName.Length);

            foreach (DirectoryInfo childDirectory in directory.GetDirectories())
            {
                FileSystemNodeValue directoryNode = new FileSystemNodeValue(childDirectory);

                base.AddLast(walkingTreeNode, directoryNode);

                this.BuildIndex(childDirectory, walkingTreeNode.LastChild.Value);
            }


            foreach (FileInfo childFile in directory.GetFiles(@"*.*"))
            {
                FileSystemNodeValue fileNode = new FileSystemNodeValue(childFile);

                base.AddLast(walkingTreeNode, fileNode);
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) B3IT
Sweden Sweden
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions