On c:
, I have tens of thousands of *.foobar
files. They\'re in all sorts of places (i.e. subdirs). These files are roughly 1 - 64 kb in size, and p
Great work, here is an extension to your code to return FileSystemInfo's instead of string paths. Some minor changes in line, like adding in SearchOption (like the native .net one has), and error trapping on initial directory get in case the root folder is access denied. Thanks again for the original posting!
public class SafeFileEnumerator : IEnumerable
{
///
/// Starting directory to search from
///
private DirectoryInfo root;
///
/// Filter pattern
///
private string pattern;
///
/// Indicator if search is recursive or not
///
private SearchOption searchOption;
///
/// Any errors captured
///
private IList errors;
///
/// Create an Enumerator that will scan the file system, skipping directories where access is denied
///
/// Starting Directory
/// Filter pattern
/// Recursive or not
public SafeFileEnumerator(string root, string pattern, SearchOption option)
: this(new DirectoryInfo(root), pattern, option)
{}
///
/// Create an Enumerator that will scan the file system, skipping directories where access is denied
///
/// Starting Directory
/// Filter pattern
/// Recursive or not
public SafeFileEnumerator(DirectoryInfo root, string pattern, SearchOption option)
: this(root, pattern, option, new List())
{}
// Internal constructor for recursive itterator
private SafeFileEnumerator(DirectoryInfo root, string pattern, SearchOption option, IList errors)
{
if (root == null || !root.Exists)
{
throw new ArgumentException("Root directory is not set or does not exist.", "root");
}
this.root = root;
this.searchOption = option;
this.pattern = String.IsNullOrEmpty(pattern)
? "*"
: pattern;
this.errors = errors;
}
///
/// Errors captured while parsing the file system.
///
public Exception[] Errors
{
get
{
return errors.ToArray();
}
}
///
/// Helper class to enumerate the file system.
///
private class Enumerator : IEnumerator
{
// Core enumerator that we will be walking though
private IEnumerator fileEnumerator;
// Directory enumerator to capture access errors
private IEnumerator directoryEnumerator;
private DirectoryInfo root;
private string pattern;
private SearchOption searchOption;
private IList errors;
public Enumerator(DirectoryInfo root, string pattern, SearchOption option, IList errors)
{
this.root = root;
this.pattern = pattern;
this.errors = errors;
this.searchOption = option;
Reset();
}
///
/// Current item the primary itterator is pointing to
///
public FileSystemInfo Current
{
get
{
//if (fileEnumerator == null) throw new ObjectDisposedException("FileEnumerator");
return fileEnumerator.Current as FileSystemInfo;
}
}
object System.Collections.IEnumerator.Current
{
get { return Current; }
}
public void Dispose()
{
Dispose(true, true);
}
private void Dispose(bool file, bool dir)
{
if (file)
{
if (fileEnumerator != null)
fileEnumerator.Dispose();
fileEnumerator = null;
}
if (dir)
{
if (directoryEnumerator != null)
directoryEnumerator.Dispose();
directoryEnumerator = null;
}
}
public bool MoveNext()
{
// Enumerate the files in the current folder
if ((fileEnumerator != null) && (fileEnumerator.MoveNext()))
return true;
// Don't go recursive...
if (searchOption == SearchOption.TopDirectoryOnly) { return false; }
while ((directoryEnumerator != null) && (directoryEnumerator.MoveNext()))
{
Dispose(true, false);
try
{
fileEnumerator = new SafeFileEnumerator(
directoryEnumerator.Current,
pattern,
SearchOption.AllDirectories,
errors
).GetEnumerator();
}
catch (Exception ex)
{
errors.Add(ex);
continue;
}
// Open up the current folder file enumerator
if (fileEnumerator.MoveNext())
return true;
}
Dispose(true, true);
return false;
}
public void Reset()
{
Dispose(true,true);
// Safely get the enumerators, including in the case where the root is not accessable
if (root != null)
{
try
{
fileEnumerator = root.GetFileSystemInfos(pattern, SearchOption.TopDirectoryOnly).AsEnumerable().GetEnumerator();
}
catch (Exception ex)
{
errors.Add(ex);
fileEnumerator = null;
}
try
{
directoryEnumerator = root.GetDirectories(pattern, SearchOption.TopDirectoryOnly).AsEnumerable().GetEnumerator();
}
catch (Exception ex)
{
errors.Add(ex);
directoryEnumerator = null;
}
}
}
}
public IEnumerator GetEnumerator()
{
return new Enumerator(root, pattern, searchOption, errors);
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}