I feel like an utter noob at this rate. My main mission for a period of time I won't embarass myself by revealing has been to get the files in each Component to list themselves out in a directory tree. As you can see, its only been going from bad to worse.
The problem is that there is some absolutely maddening self referencing going on, and while I can see the problem and where it exists, I cannot for the life of me figure out whats been going wrong. I want this over, so I'm going to turn to the people of GDNet to see if they can offer assistance on a way around it.
private void RefreshImageModel() { treeFiles.BeginUpdate(); treeFiles.Nodes.Clear(); TreeNode component, file, folder, current, next = null; Queue<String> folders = new Queue<String>(); foreach (Component c in image.Components) { component = new TreeNode(); component.Tag = "Component"; component.Text = c.Name; component.ImageIndex = 1; component.SelectedImageIndex = 1; treeFiles.Nodes.Add(component); foreach (InstallFile ifile in c.Files) { current = component; foreach(string s in ifile.RelativePath.Split('\\')) folders.Enqueue(s); foreach (string s in folders) { if (current.Nodes.Count > 0) { foreach (TreeNode tn in current.Nodes) { if (tn.Text.Equals(s)) current = tn; } if (next == null || next == current) { folder = CreateDirectory(s); current.Nodes.Add(folder); current = folder; } } else { folder = CreateDirectory(s); current.Nodes.Add(folder); current = folder; } } file = new TreeNode(); file.Tag = "File"; file.Text = ifile.Name; file.ImageIndex = 2; file.SelectedImageIndex = 2; current.Nodes.Add(file); } } treeFiles.EndUpdate(); treeFiles.ExpandAll(); }
BotH: If anyone wants to give me the thrashing I so justly deserve for that pun, drop me a PM and we can arrange a time and place.
However, here's how I'd do it:
- Assemble a list of files and their paths
- Clear out all tree nodes and add a single root node
- Iterate over the list
- Call AddTreeNode(rootnode, file) for each file
AddTreeNode(node, file) is simple:
- Find the first path separator (i.e. / or \) in the string, starting from the left
- If there is no path separator, the remaining string is a file name - add a child node to the given node with the file's name as its text, and return
- Strip off everything to the left of the separator and call it PathFragment
- Search node's children to see if there is a child matching PathFragment
- If so, keep that node as NextChild
- If not, add a new child node to node and store the new node in NextChild
- Take everything to the right of the path separator in file and call it NextFile
- Call AddTreeNode(NextChild, NextFile)
That should do what you need, assuming that what I think you need is actually what you really do need.
Oh, and I'm up for giving you that thrashing - my schedule's pretty much wide open, so whatever works for you.