-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathFileRecursive.cs
62 lines (53 loc) · 1.52 KB
/
FileRecursive.cs
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
using System.Collections.Generic;
using System.IO;
static class FileHelper
{
public static List<FileInfo> GetFilesRecursive(string b)
{
// 1.
// Store results in the file results list.
List<FileInfo> result = new List<FileInfo>();
// 2.
// Store a stack of our directories.
Stack<string> stack = new Stack<string>();
// 3.
// Add initial directory.
stack.Push(b);
// 4.
// Continue while there are directories to process
while (stack.Count > 0)
{
// A.
// Get top directory
string dir = stack.Pop();
try
{
// B
// Add all files at this directory to the result List.
string[] tmpfiles = Directory.GetFiles(dir, "*.*");
for (int i = 0; i < tmpfiles.Length; i++)
{
try
{
result.Add(new System.IO.FileInfo(tmpfiles[i]));
}
catch
{
}
}
// C
// Add all directories at this directory.
foreach (string dn in Directory.GetDirectories(dir))
{
stack.Push(dn);
}
}
catch
{
// D
// Could not open the directory
}
}
return result;
}
}