Debugging GetAllFilesAndDirectories method, when there are empty sub-directories

浪子不回头ぞ 提交于 2019-12-11 12:43:58

问题


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?


回答1:


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?




回答2:


Change || to && in your while loop.




回答3:


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




回答4:


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;
            }
        }
    }


来源:https://stackoverflow.com/questions/1547061/debugging-getallfilesanddirectories-method-when-there-are-empty-sub-directories

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!