-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSearcher.cs
155 lines (136 loc) · 4.96 KB
/
Searcher.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
namespace FindFiles
{
public class Searcher
{
private static FileAttributes _includeAttributes = FileAttributes.Normal;
private static FileAttributes _excludeAttributes = FileAttributes.ReadOnly;
public static void InitializeAttributes(FileAttributes include, FileAttributes exclude)
{
_includeAttributes = include;
_excludeAttributes = exclude;
}
/// <summary>
/// Recurses the given path, adding all matching files on that path to
/// the list box. After it finishes with the files, it
/// calls itself once for each directory on the path.
/// </summary>
public static void RecurseDirectory(string searchPath, BackgroundWorker worker)
{
if (worker.CancellationPending)
return;
string[] files;
// split searchPath into a directory and a wildcard specification
string directory = Path.GetDirectoryName(searchPath);
string searchPattern = Path.GetFileName(searchPath);
if (string.IsNullOrEmpty(searchPattern))
searchPattern = "*.*";
if (string.IsNullOrEmpty(directory))
return; // nothing to search
try
{
files = Directory.GetFiles(directory, searchPattern);
foreach (string file in files)
{
if (worker.CancellationPending)
return;
FileAttributes fileAttributes = File.GetAttributes(file);
if (IncludeFile(fileAttributes, _includeAttributes) &&
!ExcludeFile(fileAttributes, _excludeAttributes))
{
worker.ReportProgress(0, file);
}
}
}
catch (UnauthorizedAccessException)
{
return; // no access, ignore
}
catch (DirectoryNotFoundException)
{
return; // no access, ignore
}
// recurse through all subdirectories
string[] directories = Directory.GetDirectories(directory);
foreach (string subdirectory in directories)
RecurseDirectory(Path.Combine(subdirectory, searchPattern), worker);
}
private static bool IncludeFile(FileAttributes fileAttr, FileAttributes includeAttr)
{
if ((includeAttr == 0) && (fileAttr == 0))
return true; // no attributes
if (includeAttr == 0)
return false; // nothing to include
if (MatchAttribute(fileAttr, includeAttr, FileAttributes.Archive))
return true;
if (MatchAttribute(fileAttr, includeAttr, FileAttributes.Compressed))
return true;
if (MatchAttribute(fileAttr, includeAttr, FileAttributes.Device))
return true;
if (MatchAttribute(fileAttr, includeAttr, FileAttributes.Directory))
return true;
if (MatchAttribute(fileAttr, includeAttr, FileAttributes.Encrypted))
return true;
if (MatchAttribute(fileAttr, includeAttr, FileAttributes.Hidden))
return true;
if (MatchAttribute(fileAttr, includeAttr, FileAttributes.Normal))
return true;
if (MatchAttribute(fileAttr, includeAttr, FileAttributes.NotContentIndexed))
return true;
if (MatchAttribute(fileAttr, includeAttr, FileAttributes.Offline))
return true;
if (MatchAttribute(fileAttr, includeAttr, FileAttributes.ReadOnly))
return true;
if (MatchAttribute(fileAttr, includeAttr, FileAttributes.ReparsePoint))
return true;
if (MatchAttribute(fileAttr, includeAttr, FileAttributes.SparseFile))
return true;
if (MatchAttribute(fileAttr, includeAttr, FileAttributes.System))
return true;
if (MatchAttribute(fileAttr, includeAttr, FileAttributes.Temporary))
return true;
return false;
}
private static bool ExcludeFile(FileAttributes fileAttr, FileAttributes excludeAttr)
{
if (excludeAttr == 0)
return false; // nothing to exclude
if (MatchAttribute(fileAttr, excludeAttr, FileAttributes.Archive))
return true;
if (MatchAttribute(fileAttr, excludeAttr, FileAttributes.Compressed))
return true;
if (MatchAttribute(fileAttr, excludeAttr, FileAttributes.Device))
return true;
if (MatchAttribute(fileAttr, excludeAttr, FileAttributes.Directory))
return true;
if (MatchAttribute(fileAttr, excludeAttr, FileAttributes.Encrypted))
return true;
if (MatchAttribute(fileAttr, excludeAttr, FileAttributes.Hidden))
return true;
if (MatchAttribute(fileAttr, excludeAttr, FileAttributes.Normal))
return true;
if (MatchAttribute(fileAttr, excludeAttr, FileAttributes.NotContentIndexed))
return true;
if (MatchAttribute(fileAttr, excludeAttr, FileAttributes.Offline))
return true;
if (MatchAttribute(fileAttr, excludeAttr, FileAttributes.ReadOnly))
return true;
if (MatchAttribute(fileAttr, excludeAttr, FileAttributes.ReparsePoint))
return true;
if (MatchAttribute(fileAttr, excludeAttr, FileAttributes.SparseFile))
return true;
if (MatchAttribute(fileAttr, excludeAttr, FileAttributes.System))
return true;
if (MatchAttribute(fileAttr, excludeAttr, FileAttributes.Temporary))
return true;
return false;
}
private static bool MatchAttribute(FileAttributes file, FileAttributes check, FileAttributes state)
{
return (((check & state) == state) && ((file & state) == state));
}
}
}