-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDirectoryTree.cs
More file actions
66 lines (57 loc) · 1.57 KB
/
Copy pathDirectoryTree.cs
File metadata and controls
66 lines (57 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace DiskTree
{
public class DirectoryTree : IEnumerable<DirectoryTree>
{
public readonly List<DirectoryTree> Branches = new List<DirectoryTree>();
public int Level;
public string Name;
public string Path = "";
public DirectoryTree(string key)
{
Name = "ROOT";
Level = -1;
Branches.Add(new DirectoryTree
{
Name = key,
Level = 0,
Path = key + "\\"
});
}
public DirectoryTree()
{
Name = "ROOT";
Level = -1;
}
public void Add(string key)
{
if (Branches.Any(b => b.Name == key))
return;
Branches.Add(new DirectoryTree
{
Name = key,
Level = Level + 1,
Path = Path + key + "\\"
});
}
public IEnumerator<DirectoryTree> GetEnumerator()
{
var stack = new Stack<DirectoryTree>();
foreach (var item in Branches)
stack.Push(item);
while (stack.Count != 0)
{
var current = stack.Pop();
yield return current;
foreach (var directoryTree in current.Branches)
stack.Push(directoryTree);
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
}