Debugging the GetAllFilesAndDirectories method when there are empty subdirectories

This method works to retrieve files, directories, and subdirectories and their contents IF one of the subdirectories is 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:

The System.InvalidOperationException fix was an unhandled Message = "Stack empty." Source = "System"

Any ideas how to fix it?

+1
source share
4 answers

I added another answer to the question that you asked a little back , which will help you avoid this, because you will use a completely different (and easier to read) bit of code than the one you use here.

However, to save time searching, check out this article for another way to read files / directories recursively.

http://support.microsoft.com/kb/303974

I understand that this is not the answer to this specific question, but why spend time fixing a solution that does not work well when there is a completely viable solution that is easier to handle?

0
source

Change || to && in your while loop.

+1
source

I can suggest this code:

 static IEnumerable<FileSystemInfo> GetAllFilesAndDirectories(string path) { string currentDirectory = ""; string[] files = Directory.GetFiles( // skip empty subfolders path, "*.*", SearchOption.AllDirectories); foreach (string file in files) { if(currentDirectory != Path.GetDirectoryName(file)) { // First time in this directory: return it currentDirectory = Path.GetDirectoryName(file); yield return new DirectoryInfo(currentDirectory); } yield return new FileInfo(file); } } static void Main(string[] args) { foreach (FileSystemInfo info in GetAllFilesAndDirectories(@"c:\intel")) { Console.WriteLine("{0} ({1})", info.FullName, info.Attributes); } } 

This will display:

  c: \ intel \ Logs (Directory)
 c: \ intel \ Logs \ IntelChipset.log (Archive)
 c: \ intel \ Logs \ IntelGFX.log (Archive)
 c: \ intel \ Logs \ IntelStor.log (Archive)
 c: \ intel \ ExtremeGraphics \ CUI \ Resource (Directory)
 c: \ intel \ ExtremeGraphics \ CUI \ Resource \ igfxres.dll (Archive)

If you want List<> , just call the .ToList() method

0
source
 while (stack.Count > 0) 

your time is working with the stack.
even when you start collecting from a subdirectory, the stack is not empty.

but when you collect from an empty subdirectory and a subdirectory at the end of the list of directories, your stack is empty and there is no reason to continue the search.
full fixed code:

  public static IEnumerable<FileSystemInfo> GetAllFilesAndDirectories(string dir) { DirectoryInfo dirInfo = new DirectoryInfo(dir); Stack<FileSystemInfo> stack = new Stack<FileSystemInfo>(); stack.Push(dirInfo); while (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); } } else { yield return fileSystemInfo; } } } 
0
source

All Articles