Using .NET 3.0, I've got the method below which correctly returns a collection of all of the files and directories (and sub-directories) of a specified directory. I'd like to, if possible, dumb this down to only use constructs that I'm pretty comfortable with. Specifically, here are the things I'm not clear on:
1. IEnumerable<FileSystemInfo>: I'd like to return List<FileSystemInfo> instead
2. Stack<FileSystemInfo>: I'd list to use List<FileSystemInfo> instead.
3. yield return: I've never used this before
.
public static IEnumerable<FileSystemInfo> GetAllFilesAndDirectories ( string dir ) {
DirectoryInfo dirInfo = new DirectoryInfo( dir );
Stack<FileSystemInfo> stack = new Stack<FileSystemInfo>();
stack.Push( dirInfo );
while ( dirInfo != null || stack.Count > 0 ) {
FileSystemInfo fileSystemInfo = stack.Pop();
DirectoryInfo subDirectoryInfo = fileSystemInfo as DirectoryInfo;
if ( subDirectoryInfo != null ) {
yield return subDirectoryInfo;
foreach ( FileSystemInfo fsi in subDirectoryInfo.GetFileSystemInfos() ) {
stack.Push( fsi );
}
dirInfo = subDirectoryInfo;
} else {
yield return fileSystemInfo;
dirInfo = null;
}
}
}
.
The argument could be made that I should just get comfortable with the code above, but that's not what I'm shooting for today.
Thanks in advance
Here is the shortest way I can think of:
static List<FileSystemInfo> GetAllFilesAndDirectories(string dir)
{
DirectoryInfo dirInfo = new DirectoryInfo(dir);
List<FileSystemInfo> allFilesAndDirectories = new List<FileSystemInfo>();
allFilesAndDirectories.AddRange(dirInfo.GetFiles("*", SearchOption.AllDirectories));
allFilesAndDirectories.AddRange(dirInfo.GetDirectories("*", SearchOption.AllDirectories));
return allFilesAndDirectories;
}
It will return a list of all files and directories at all levels, starting from the given path. The order it returns them in would be all files, then all directories.
I believe you're looking for the existing method GetFileSystemInfos(string, SearchOptions). If you specify AllDirectories as the SearchOptions value it will recursively search the passed in folder.
For Example:
public static List<FileSystemInfo> GetAllFilesAndDirectories ( string dir ) {
DirectoryInfo info = new DirectoryInfo(dir);
FileSystemInfo[] all = info.GetFileSystemInfos("*", SearchOptions.AllDirectories);
return new List<FileSystemInfo>(all);
}
If you want to write it out the long way though you can do the following
public static List<FileSystemInfo> GetAllFilesAndDirectories ( string dir ) {
int i = 0;
List<DirectoryInfo> toProcess = new List<DirectoryInfo>();
List<FileSystemInfo> list = new List<FileSystemInfo>();
toProcess.Add(new DirectoryInfo(dir));
while ( i < toProcess.Count ) {
DirectoryInfo curDir = toProcess[i];
foreach ( FileSystemInfo curFile in curDir.GetFileSystemInfos() ) {
list.Add(curFile);
DirectoryInfo maybe = curFile as DirectoryInfo;
if ( maybe != null ) {
toProcess.Add(maybe);
}
i++;
}
return list;
}
FileSystemInfo[] all = info.GetFileSystemInfos("*", SearchOptions.AllDirectories);
return new List<FileSystemInfo>(all);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With