예제 #1
0
        /// <summary>
        /// Adds a child to this and continues adding recursively if there are more portions in the path
        /// </summary>
        /// <param name="parent">Parent for the added child</param>
        /// <param name="path">List of strings defining the path</param>
        ///
        protected void AddNode(FileTreeNode parent, List <string> path)
        {
            var node = parent.GetChild(path.First());

            if (path.Count() > 1)
            {
                AddNode(node, path.Skip(1).ToList());
            }
        }
예제 #2
0
        /// <summary>
        /// Fetch a child by name, create one if it doesn't exist.
        /// </summary>
        /// <param name="name">Name of child</param>
        ///
        public FileTreeNode GetChild(string name)
        {
            if (Children.TryGetValue(name, out var node))
            {
                return(node);
            }

            var newNode = new FileTreeNode()
            {
                Name = name
            };

            Children.Add(name, newNode);
            return(newNode);
        }
예제 #3
0
        public override FileTreeNode Parse()
        {
            //Root gets name from file
            var root = new FileTreeNode {
                Name = Path.GetFileNameWithoutExtension(_file)
            };

            if (!File.Exists(_file))
            {
                throw new Exception($"Json data not found: {_file}");
            }

            //Deserialize to list of strings and add them to the root node recursively

            JsonConvert.DeserializeObject <List <string> >(File.ReadAllText(_file)).
            ForEach(f => AddNode(root, f.Split('/').ToList()));

            return(root);
        }