public static FileTree ForDirectory(string rootDirectory) { FileTree root = new FileTree(rootDirectory); foreach (string file in Directory.EnumerateFileSystemEntries(rootDirectory, "*", SearchOption.AllDirectories)) { string[] parts = file.Split('/', '\\'); bool skip = false; bool isFile = File.Exists(file); int fileNameParts = isFile ? 1 : 0; for (int i = 1; !skip && i < parts.Length - fileNameParts; ++i) { if (DirectoryIgnoreList.Contains(parts[i], StringComparer.OrdinalIgnoreCase)) { skip = true; } } if (!skip) { root.GetModelFor(file, isFile); } } root.SortChildren(); return(root); }
public FileTreeModel(string fullPath, FileTree root, bool isFile) { this.IsExpanded = true; this._children = new List <FileTreeModel>(); this.Root = root; this.Name = Path.GetFileName(fullPath); this.IsFile = isFile; this.FullPath = fullPath; int lastSlash = fullPath.LastIndexOfAny(new[] { '/', '\\' }); //Normally this would be -1, but if the path starts with / or \, we don't want to make an empty entry if (lastSlash > 0) { string parentFullPath = fullPath.Substring(0, lastSlash).Trim('/', '\\'); if (!string.IsNullOrEmpty(parentFullPath)) { this.Parent = root.GetModelFor(parentFullPath, false); this.Parent?.Children?.Add(this); } } }