This method works to get the files, directories and sub-directories and their contents, UNLESS one of the sub-directories happens to be empty.
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 exception is:
System.InvalidOperationException was unhandled Message="Stack empty." Source="System"
Any ideas how to fix?
I can suggest this code:
This will display:
If you wanna a
List<>
, just call.ToList()
methodI added another answer to the question you asked a little while ago that will help you avoid this because you'd be using a completely different (and easier to read) bit of code than what you're using here.
However, to save you the time of looking, check out this article for a different way to recursively read files/directories.
http://support.microsoft.com/kb/303974
I realize this isn't an answer to this specific question, but why spend time fixing a solution that's buggy when there's a perfectly viable solution that's easier to deal with?
your while is working with stack.
even when you starting to collect from sub directory, the stack isn't empty.
but when you collecting from empty sub directory and sub directory at the end of directory list, your stack is empty, and there is no reason for continue to search.
the full fixed code is :
Change || to && in your while loop.