Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Populate TreeView from a flat file

Populate TreeView from a flat file

Scheduled Pinned Locked Moved C#
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    ccsalway
    wrote on last edited by
    #1

    Hi people! I have been trying for days to populate a treeview from a particular structure. The text file i have to work with is similar to this: root root/folder1 root/folder2 root/folder2/subfolder1 root/folder2/subfolder2/subfolder3 root/folder3/subfolder4 root/folder4 Which I can almost get to populate a treeview with. In fact, i can get to root/folder2/subfolder2/subfolder3, and then it wont display the root/folder3/subfolder4 because it hasnt created the root/folder3 path first! There must be an easier way to do this!!! Here's my '2 evenings of working on this now', code so far:

    ArrayList paths = {the file split line by line into an arraylist}

    private void Run()
    {
    treeView1.Nodes.Add("root");
    Buildtree("root", treeView1.Nodes[0]);
    }

    private void Buildtree(String path, TreeNode parentNode)
    {
    ArrayList directories = GetDirectories(path);
    if (directories.Count != 0)
    {
    foreach (string directory in directories)
    {
    TreeNode node = new TreeNode(directory.Remove(0, path.Length + 1));
    parentNode.Nodes.Add(node);

            Buildtree(directory, node);
        }
    }
    

    }

    private ArrayList GetDirectories(string parentPath)
    {
    ArrayList directories = new ArrayList();
    foreach (string path in paths)
    {
    if (path.StartsWith(parentPath))
    {
    bool hasSubdir = false;

            if (path.Contains('/') && (path.Length > parentPath.Length))
            {
                int startpos = parentPath.Length + 1;
                string leftover = path.Substring(startpos, path.Length - startpos);
                if (!string.IsNullOrEmpty(leftover) && leftover.Contains('/')) hasSubdir = true;
            }
    
            if (!hasSubdir && (path != parentPath))
                directories.Add(path);
        }
    }
    return directories;
    

    }

    Im going bonkers over this!!

    P L 2 Replies Last reply
    0
    • C ccsalway

      Hi people! I have been trying for days to populate a treeview from a particular structure. The text file i have to work with is similar to this: root root/folder1 root/folder2 root/folder2/subfolder1 root/folder2/subfolder2/subfolder3 root/folder3/subfolder4 root/folder4 Which I can almost get to populate a treeview with. In fact, i can get to root/folder2/subfolder2/subfolder3, and then it wont display the root/folder3/subfolder4 because it hasnt created the root/folder3 path first! There must be an easier way to do this!!! Here's my '2 evenings of working on this now', code so far:

      ArrayList paths = {the file split line by line into an arraylist}

      private void Run()
      {
      treeView1.Nodes.Add("root");
      Buildtree("root", treeView1.Nodes[0]);
      }

      private void Buildtree(String path, TreeNode parentNode)
      {
      ArrayList directories = GetDirectories(path);
      if (directories.Count != 0)
      {
      foreach (string directory in directories)
      {
      TreeNode node = new TreeNode(directory.Remove(0, path.Length + 1));
      parentNode.Nodes.Add(node);

              Buildtree(directory, node);
          }
      }
      

      }

      private ArrayList GetDirectories(string parentPath)
      {
      ArrayList directories = new ArrayList();
      foreach (string path in paths)
      {
      if (path.StartsWith(parentPath))
      {
      bool hasSubdir = false;

              if (path.Contains('/') && (path.Length > parentPath.Length))
              {
                  int startpos = parentPath.Length + 1;
                  string leftover = path.Substring(startpos, path.Length - startpos);
                  if (!string.IsNullOrEmpty(leftover) && leftover.Contains('/')) hasSubdir = true;
              }
      
              if (!hasSubdir && (path != parentPath))
                  directories.Add(path);
          }
      }
      return directories;
      

      }

      Im going bonkers over this!!

      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #2
      1. Why are you using ArrayList instead of List? 1) That should be easy. 1.0) Use String.Split to split the string on '//' 1.1) Enumerate the resultant strings creating Nodes as necessary
      1 Reply Last reply
      0
      • C ccsalway

        Hi people! I have been trying for days to populate a treeview from a particular structure. The text file i have to work with is similar to this: root root/folder1 root/folder2 root/folder2/subfolder1 root/folder2/subfolder2/subfolder3 root/folder3/subfolder4 root/folder4 Which I can almost get to populate a treeview with. In fact, i can get to root/folder2/subfolder2/subfolder3, and then it wont display the root/folder3/subfolder4 because it hasnt created the root/folder3 path first! There must be an easier way to do this!!! Here's my '2 evenings of working on this now', code so far:

        ArrayList paths = {the file split line by line into an arraylist}

        private void Run()
        {
        treeView1.Nodes.Add("root");
        Buildtree("root", treeView1.Nodes[0]);
        }

        private void Buildtree(String path, TreeNode parentNode)
        {
        ArrayList directories = GetDirectories(path);
        if (directories.Count != 0)
        {
        foreach (string directory in directories)
        {
        TreeNode node = new TreeNode(directory.Remove(0, path.Length + 1));
        parentNode.Nodes.Add(node);

                Buildtree(directory, node);
            }
        }
        

        }

        private ArrayList GetDirectories(string parentPath)
        {
        ArrayList directories = new ArrayList();
        foreach (string path in paths)
        {
        if (path.StartsWith(parentPath))
        {
        bool hasSubdir = false;

                if (path.Contains('/') && (path.Length > parentPath.Length))
                {
                    int startpos = parentPath.Length + 1;
                    string leftover = path.Substring(startpos, path.Length - startpos);
                    if (!string.IsNullOrEmpty(leftover) && leftover.Contains('/')) hasSubdir = true;
                }
        
                if (!hasSubdir && (path != parentPath))
                    directories.Add(path);
            }
        }
        return directories;
        

        }

        Im going bonkers over this!!

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        Hi, I just finished this little article[^] which may interest you. :)

        Luc Pattyn [Forum Guidelines] [My Articles]


        The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


        C 2 Replies Last reply
        0
        • L Luc Pattyn

          Hi, I just finished this little article[^] which may interest you. :)

          Luc Pattyn [Forum Guidelines] [My Articles]


          The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


          C Offline
          C Offline
          ccsalway
          wrote on last edited by
          #4

          Woo Hoo!! Luc, Thank you VERY much! You're amazing! And thank you to all the others (one person) who came back with a slightly subdued answer. Now to try the code myself...

          L 1 Reply Last reply
          0
          • C ccsalway

            Woo Hoo!! Luc, Thank you VERY much! You're amazing! And thank you to all the others (one person) who came back with a slightly subdued answer. Now to try the code myself...

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #5

            Hi, I just updated the little article[^] as I realized recursion wasn't really needed, a simple loop is sufficient. :)

            Luc Pattyn [Forum Guidelines] [My Articles]


            The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


            1 Reply Last reply
            0
            • L Luc Pattyn

              Hi, I just finished this little article[^] which may interest you. :)

              Luc Pattyn [Forum Guidelines] [My Articles]


              The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


              C Offline
              C Offline
              ccsalway
              wrote on last edited by
              #6

              Much better, smaller, cleaner, just the way I like it! Many thanks, Luc

              L 1 Reply Last reply
              0
              • C ccsalway

                Much better, smaller, cleaner, just the way I like it! Many thanks, Luc

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #7

                You're welcome. :)

                Luc Pattyn [Forum Guidelines] [My Articles]


                The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups