My favorites | Sign in
Project Home Downloads Issues Source
Repository:
Checkout   Browse   Changes   Clones    
 
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#region Copyright 2008-2011 by Roger Knapp, Licensed under the Apache License, Version 2.0
/* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#endregion
using System;
using System.Collections.Generic;
using System.IO;

namespace CSharpTest.Net.Utils
{
/// <summary>
/// A utility class for gathering files
/// </summary>
[System.Diagnostics.DebuggerNonUserCode]
partial class FileList : System.Collections.ObjectModel.KeyedCollection<string, FileInfo>
{
bool _recurse = true;
bool _ignoreDirAttrs = false;
FileAttributes _prohibitAttrib = FileAttributes.Hidden | FileAttributes.Offline | FileAttributes.System;

/// <summary>
/// Creates an empty FileList
/// </summary>
public FileList()
{ }

/// <summary>
/// Constructs a FileList containing the files specified or found within the directories
/// specified. See Add(string) for more details.
/// </summary>
public FileList(params string[] filesOrDirectories)
: base(StringComparer.OrdinalIgnoreCase, 0)
{
Add(filesOrDirectories);
}

/// <summary>
/// Constructs a FileList containing the files specified or found within the directories
/// specified. See Add(string) for more details. Files and directories that contain the
/// attribtes defined in prohibitedAttributes will be ignored, use '0' for everything.
/// </summary>
public FileList(FileAttributes prohibitedAttributes, params string[] filesOrDirectories)
: base(StringComparer.OrdinalIgnoreCase, 0)
{
_prohibitAttrib = prohibitedAttributes;
Add(filesOrDirectories);
}

/// <summary>
/// Creates a list containing the specified FileInfo records.
/// </summary>
public FileList(params FileInfo[] copyFrom)
{
if (copyFrom == null) throw new ArgumentNullException();
foreach (FileInfo finfo in copyFrom)
AddFile(finfo);
}
#region Public Properties
/// <summary>
/// Gets or sets a value that allows traversal of all directories added.
/// </summary>
public bool RecurseFolders { get { return _recurse; } set { _recurse = value; } }
/// <summary>
/// Setting this will greatly improve performance at the cost of not evaluating filters on directories
/// </summary>
public bool IgnoreFolderAttributes { get { return _ignoreDirAttrs; } set { _ignoreDirAttrs = value; } }
/// <summary>
/// Set this to the set of attributes that if a directory or file contains should be skipped. For
/// example when set to FileAttributes.Hidden, hidden files and folders will be ignored.
/// </summary>
public FileAttributes ProhibitedAttributes { get { return _prohibitAttrib; } set { _prohibitAttrib = value; } }
#endregion Public Properties

/// <summary>
/// Adds a set of items to the collection, see Add(string) for details.
/// </summary>
public void Add(params string[] filesOrDirectories)
{
if (filesOrDirectories == null) throw new ArgumentNullException();
foreach (string fd in filesOrDirectories)
Add(fd);
}

/// <summary>
/// Adds the specified file to the collection. If the item specified is a directory
/// that directory will be crawled for files, and optionally (RecurseFolders) child
/// directories. If the name part of the path contains wild-cards they will be
/// considered throughout the folder tree, i.e: C:\Temp\*.tmp will yeild all files
/// having an extension of .tmp. Again if RecurseFolders is true you will get all
/// .tmp files anywhere in the C:\Temp folder.
/// </summary>
public void Add(string fileOrDirectory)
{
if (fileOrDirectory == null) throw new ArgumentNullException();

if (!Path.IsPathRooted(fileOrDirectory))
fileOrDirectory = Path.Combine(Environment.CurrentDirectory, fileOrDirectory);

if (File.Exists(fileOrDirectory))
AddFile(new FileInfo(fileOrDirectory));
else if (Directory.Exists(fileOrDirectory))
AddDirectory(new DirectoryInfo(fileOrDirectory), "*");
else
{
string filePart = Path.GetFileName(fileOrDirectory);
string dirPart = Path.GetDirectoryName(fileOrDirectory);

//if it is a valid directory and the file exists in the search area, then pass
//it on to the filters, if it doesn't exist throw not found.
if (Directory.Exists(dirPart) && (filePart.IndexOfAny(new char[] { '?', '*' }) >= 0 ||
Directory.GetFiles(dirPart, filePart, RecurseFolders ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly).Length > 0
) )
{
AddDirectory(new DirectoryInfo(dirPart), filePart);
}
else
throw new FileNotFoundException("File not found.", fileOrDirectory);
}
}

/// <summary>
/// Returns true if the given file is in the collection
/// </summary>
public new bool Contains(FileInfo file)
{
return Dictionary != null && Dictionary.ContainsKey(file.FullName);
}

/// <summary>
/// Adds one or files to the collection
/// </summary>
public void AddRange(params FileInfo[] files)
{
foreach (FileInfo f in files)
{
if (!Contains(f))
Add(f);
}
}

/// <summary>
/// Remove the files specified if they exist in the collection
/// </summary>
public void Remove(params FileInfo[] files)
{
foreach (FileInfo finfo in files)
base.Remove(finfo.FullName);
}

/// <summary>
/// Returns the collection of FileInfo as an array
/// </summary>
public FileInfo[] ToArray()
{
return new List<FileInfo>(base.Items).ToArray();
}

/// <summary>
/// Converts all FileInfo elements into their fully-qualified file names
/// </summary>
public string[] GetFileNames()
{
if (base.Dictionary == null)
return new string[0];
return new List<String>(base.Dictionary.Keys).ToArray();
}

#region Private / Protected Implementation

private void AddFile(FileInfo file)
{
if (!Allowed(file) || (base.Dictionary != null && base.Dictionary.ContainsKey(file.FullName)))
return;

if (FileFound != null)
{
FileFoundEventArgs args = new FileFoundEventArgs(false, file);
FileFound(this, args);
if (args.Ignore)
return;
}
base.Add(file);
}

private void AddDirectory(DirectoryInfo dir, string match)
{
if (!_ignoreDirAttrs && !Allowed(dir))
return;

SearchOption deepMatch = SearchOption.TopDirectoryOnly;
if (_recurse && (_ignoreDirAttrs == true || _prohibitAttrib == 0))
deepMatch = SearchOption.AllDirectories;

foreach (FileInfo f in dir.GetFiles(match, deepMatch))
AddFile(f);

if (_recurse && deepMatch != SearchOption.AllDirectories)
{
foreach (DirectoryInfo child in dir.GetDirectories())
AddDirectory(child, match);
}
}

private bool Allowed(FileSystemInfo item)
{
if ((_prohibitAttrib & item.Attributes) != 0)
return false;

return true;
}

/// <summary>
/// The key for the specified element.
/// </summary>
protected sealed override string GetKeyForItem(FileInfo item)
{
if (item == null) throw new ArgumentNullException();
return item.FullName;
}

#endregion

#region FileFoundEvent

/// <summary>
/// Raised when a new file is about to be added to the collection, set e.Ignore
/// to true will cancel the addition of this file.
/// </summary>
public event EventHandler<FileFoundEventArgs> FileFound;

/// <summary>
/// Event args passed to the FileFound event
/// </summary>
public class FileFoundEventArgs : EventArgs
{
/// <summary>
/// Allows manually filtering a file by setting Ignore=true;
/// </summary>
public bool Ignore;
/// <summary>
/// Provides access to the FileInfo of this item
/// </summary>
public readonly FileInfo File;

/// <summary>
/// Constructs the event args
/// </summary>
public FileFoundEventArgs(bool ignore, FileInfo file)
{
this.Ignore = ignore;
this.File = file;
}
}

#endregion FileFoundEvent
}
}

Change log

59104bd26e63 by rogerk on Apr 26, 2011   Diff
Release version 1.11.426.305
Go to: 
Project members, sign in to write a code review

Older revisions

a9af4ee4f5f9 by Grigand on Oct 24, 2010   Diff
*v1.10.1024.336*

  # Added
CSharpTest.Net.Generators.exe to
integrate with the CmdTool's VS
...
d55997422b7a by Grigand on Apr 20, 2010   Diff
Release version 1.10.420.164
f098da8efab8 by grigand on Oct 4, 2009   Diff
Released version 1.9.1004.144

Major updates include:
1. Removed dependency from Library to
Logging so that they can be
...
All revisions of this file

File info

Size: 8643 bytes, 267 lines
Powered by Google Project Hosting