views:

203

answers:

4

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?

A: 

I 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?

David Stratton
A: 

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 wanna a List<>, just call .ToList() method

Rubens Farias
A: 
while (stack.Count > 0)

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 :

    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;
            }
        }
    }
Avram
+1  A: 

Change || to && in your while loop.

s_hewitt