public virtual VMNode FindFile(string path) { string s = path; VMNode t = null; if (Nodes.Count > 0) { foreach (VMNode directory in Nodes) { t = SearchTreeView(directory, s); if (t != null) { t.Selected = true; t.Expanded = true; return(t); } } } return(t); }
public void LoadDirectoriesToTree() { cachedNodes.Clear(); foreach (FileDirectory fileDirectory in fileLoader.Directories) { VMNode root = PopulateNodesFromFileDirectory(fileDirectory); if (root == null) { continue; } cachedNodes.Add(root); } if (cachedNodes.Count == 0) { return; } cachedNodes[0].Expanded = true; Nodes = cachedNodes; }
//dep. Switch to the FileDirectory version below private VMNode PopulateNodesFromDirectory(string directory) { VMNode root = new VMNode(); root.Name = directory; root.FilePath = directory; List <string> files = fileLoader.LoadFiles(directory); // List<string> files = tool.LoadAudioFiles(directory, SearchOption.AllDirectories); if (files.Count == 0) { return(null); } foreach (string f in files) { root.BuildChildNodes(directory, f, f); } root.SortNodes(); return(root); }
public void AddNode(VMNode node) { node.Parent = this; Nodes.Add(node); }
//recursively build child nodes based on the filepath internal void BuildChildNodes(string directory, string fullPath, string subPath) { //remove everything before the directory in the path. We could shift this responsibility to //the root caller of this function instead, since it's only relevant to the initial fullpath string[] dir_splitter = new string[] { directory }; string[] file_part = subPath.Split(dir_splitter, StringSplitOptions.RemoveEmptyEntries); //we always expect file_part.length to be 1, even if the functin is being called //recursively and subpath's directory has already been removed if (file_part.Length == 0) { tool.show(10, "Assert: we should always remain with one substring after removing directory"); return; } if (file_part.Length != 1) { tool.show(10, "Assert: Expecting subPath.Split(Directory) to always return 1 string", "", "Path:", "", fullPath, "", "SubPath: ", subPath); tool.show(10, "Assert: file_part.Length", "", file_part.Length); foreach (string s in file_part) { tool.show(5, "Assert: filePart: ", s); } return; } string substring = file_part[0]; //split the string by file seperators string[] use_slashes = new string[] { @"\" }; string[] parts = substring.Split(use_slashes, StringSplitOptions.RemoveEmptyEntries); //we should always have at least one entry if (parts.Length == 0) { tool.show(10, "Assert: Something might have gone wrong: we're not expecting string.split to return an empty array", "SubString:", substring, "FilePart[0]", file_part[0], "Subpath", subPath, "FullPath", fullPath); return; } //if there is only one part left then we have found the file/last folder if (parts.Length == 1) { VMNode node = new VMNode(); node.Name = parts[0]; // TreePath = node.Name; node.FilePath = fullPath; node.IsFile = true; AddNode(node); return; } //this is the next consecutive folder in the branch string nextPath = parts[0]; VMNode child; //Does this node alredy have a child with the given folder name/path? //If so use it, otherwise create a new node if (!_nodePaths.TryGetValue(nextPath, out child)) { child = new VMNode(); child.Name = nextPath; //construct the folder path string[] folder_splitter = new string[] { parts[1] }; string[] folder_part = fullPath.Split(folder_splitter, StringSplitOptions.RemoveEmptyEntries); //remove the trailer \ child.FilePath = folder_part[0].Remove(folder_part[0].Count() - 1); // child.TreePath = nextPath; _nodePaths[nextPath] = child; AddNode(child); } //reconstruct the sub-folder path, skipping the folder/child we just created string newsubstring = ""; for (int i = 1; i < parts.Length; i++) { newsubstring = string.Concat(newsubstring, @"\", parts[i]); } //continue building the tree structure from this child based on the newly shortened path child.BuildChildNodes(directory, fullPath, newsubstring); }