Web Analytics Made Easy -
StatCounter Post a C#: File System TreeView - CodingForum

Announcement

Collapse
No announcement yet.

Post a C#: File System TreeView

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Post a C#: File System TreeView

    Code:
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;
    
    namespace FSTreeExample
    {
        public class FSTree : TreeView
        {
            /// <summary>
            /// a simple File System treeview component
            /// populates each level on view,
            /// then destroys the nodes after the level is closed.
            /// </summary>
    
            public FSTree()
            {
                this.BeforeExpand += new TreeViewCancelEventHandler(FSTree_BeforeExpand);
                this.AfterCollapse += new TreeViewEventHandler(FSTree_AfterCollapse);
    
            }
    
            private void FSTree_AfterCollapse(object sender, TreeViewEventArgs e)
            {
                e.Node.Nodes.Clear();
                e.Node.Nodes.Add("."); // need a node in there to spark the expand.
            }
    
            private void FSTree_BeforeExpand(object sender, TreeViewCancelEventArgs e)
            {
                try
                {
                    e.Node.Nodes.Clear();
                    string needle = (string)e.Node.Tag;
                    string[] folders = Directory.GetDirectories(needle);
                    string[] files = Directory.GetFiles(needle);
    
                    foreach (string folder in folders)
                    {
                        try
                        {
                            string[] s_folders = Directory.GetDirectories(folder);
                            string[] s_files = Directory.GetFiles(folder);
    
                            TreeNode t_folder = getNode(folder);
                            foreach (string s_folder in s_folders)
                            {
                                t_folder.Nodes.Add(getNode(s_folder));
                            }
                            foreach (string s_file in s_files)
                            {
                                t_folder.Nodes.Add(getNode(s_file));
                            }
                            e.Node.Nodes.Add(t_folder);
                        }
                        catch (IOException ioe)
                        {
                            // do nothing
                        }
                        catch (UnauthorizedAccessException uae)
                        {
                            // do nothing
                        }
                        finally
                        {
                            // do nothing
                        }
                    }
    
                    foreach (string file in files)
                    {
                        e.Node.Nodes.Add(getNode(file));
                    }
                }
                catch (IOException ioe)
                {
                    // do nothing
                }
                catch (UnauthorizedAccessException uae)
                {
                    // do nothing
                }
                finally
                {
                    // do nothing
                }
            }
    
            public void Load(string needle)
            {
                this.Nodes.Clear();
                TreeNode nNode = getNode(needle);
                nNode.Nodes.Add("."); // need a node in there to spark the expand.
                this.Nodes.Add(nNode);
                this.Nodes[0].Expand();
            }
    
            private TreeNode getNode(string path)
            {
                TreeNode rNode = new TreeNode();
                int limit = path.LastIndexOf('\\') + 1;
                string name = path.Substring(limit, (path.Length - limit));
                if (name.Length < 1)
                {
                    name = path.Substring(0, (path.Length - 1));
                }
                rNode.Text = name;
                rNode.Tag = path;
                return rNode;
            }
        }
    }
    simple treeview control that loads with the file system

    to use.

    Code:
    string startPath = "C:\\";
    FSTree fTree = new FSTree();
    fTree.Load((string)startPath);
    public string ConjunctionJunction(string words, string phrases, string clauses)
    {
    return (String)(words + phrases + clauses);
    }
    <--- Was I Helpfull? Let me know ---<

  • #2
    Nice... But I have VC++ 7.0 .NET... lol

    Comment


    • #3
      /nitpick
      You don't need to typecast startPath to a string
      -Brando
      Why using tables for eating is stupid!

      Comment


      • #4
        Originally posted by Brandoe85 View Post
        /nitpick
        You don't need to typecast startPath to a string

        yea i know, i was moreso putting it there for others sake..

        and there is alot that is missing / incomplete from this script.

        later versions will include the shell icons. and you will not have to 'seed' the tree with a directory. i eventually will have it list all logical devices and drives.
        public string ConjunctionJunction(string words, string phrases, string clauses)
        {
        return (String)(words + phrases + clauses);
        }
        <--- Was I Helpfull? Let me know ---<

        Comment


        • #5
          have anyone the version with the shell icons?
          i need it

          Comment

          Working...
          X