views:

1315

answers:

5

Hi,

I'm trying to populate a treeview from a list of folder path, for example:

C:\WINDOWS\addins
C:\WINDOWS\AppPatch
C:\WINDOWS\AppPatch\MUI
C:\WINDOWS\AppPatch\MUI\040C
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\MUI
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\MUI\0409

with an ouput like this:

├───addins
├───AppPatch
│   └───MUI
│       └───040C
├───Microsoft.NET
│   └───Framework
│       └───v2.0.50727
│           └───MUI
│               └───0409

Notice there's no 'C:\WINDOWS\Microsoft.NET' or 'C:\WINDOWS\Microsoft.NET\Framework' in the list. I've been working on this for almost two days and there's a bunch of bug in my code. Hope I can get help from here.

Thanks.

Eric

+1  A: 

Same as this question

PaulB
A: 

See if this helps. http://aspnet.4guysfromrolla.com/articles/083006-1.aspx

David Stratton
A: 

Here's some very old code I once used to create an ASP.NET treeview from code (assuming TreeView has an ID of TreeViewFolders):

protected void Page_Load(object sender, EventArgs e)
{
 GenerateTreeView(@"C:\WINDOWS\");
}

private void GenerateTreeView(string rootPath)
{
 GetFolders(System.IO.Path.GetFullPath(rootPath), TreeViewFolders.Nodes);
 TreeViewFolders.ExpandDepth = 1;
}

// recursive method to load all folders and files into tree
private void GetFolders(string path, TreeNodeCollection nodes)
{
 // add nodes for all directories (folders)
 string[] dirs = Directory.GetDirectories(path);
 foreach (string p in dirs)
 {
  string dp = p.Substring(path.Length);
  nodes.Add(Node("", p.Substring(path.Length), "folder"));
 }

 // add nodes for all files in this directory (folder)
 string[] files = Directory.GetFiles(path, "*.*");
 foreach (string p in files)
 {
  nodes.Add(Node(p, p.Substring(path.Length), "file"));
 }

 // add all subdirectories for each directory (recursive)
 for (int i = 0; i < nodes.Count; i++)
 {
  if (nodes[i].Value == "folder")
   GetFolders(dirs[i] + "\\", nodes[i].ChildNodes);
 }
}

// create a TreeNode from the specified path, text and type
private TreeNode Node(string path, string text, string type)
{
 TreeNode n = new TreeNode();
 n.Value = type;
 n.Text = text;
 return n;
}
Dan Diplo
A: 

Unless you post the code, its impossible to determine whats wrong with it. Instead of spending days on this why not use a 3rd party control such as the FolderView

logicnp
A: 
private void Form1_Load(object sender, EventArgs e)
    {
        var paths = new List<string>
                        {
                            @"C:\WINDOWS\AppPatch\MUI\040C",
                            @"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727",
                            @"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\MUI",
                            @"C:\WINDOWS\addins",
                            @"C:\WINDOWS\AppPatch",
                            @"C:\WINDOWS\AppPatch\MUI",
                            @"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\MUI\0409"
                        };

        treeView1.PathSeparator = @"\";

        PopulateTreeView(treeView1, paths, '\\');
}


private static void PopulateTreeView(TreeView treeView, IEnumerable<string> paths, char pathSeparator)
    {
        TreeNode lastNode = null;
        string subPathAgg;
        foreach (string path in paths)
        {
            subPathAgg = string.Empty;
            foreach (string subPath in path.Split(pathSeparator))
            {
                subPathAgg += subPath + pathSeparator;
                TreeNode[] nodes = treeView.Nodes.Find(subPathAgg, true);
                if (nodes.Length == 0)
                    if (lastNode == null)
                        lastNode = treeView.Nodes.Add(subPathAgg, subPath);
                    else
                        lastNode = lastNode.Nodes.Add(subPathAgg, subPath);
                else
                    lastNode = nodes[0];
            }
        }
    }

alt text

ehosca