Helper - IEnumerable DirectoryEx.GetFiles helper

[title]IEnumerable DirectoryEx.GetFiles helper[/title]
This is a helper class that uses reflection to enumerate files at the given path. Microframework version Directory.GetFiles gets OutOfMemoryException for folders with too many files. This class helps to avoid that.

Edit: Update for forum entry.

After using your class, I found some problems that I solved with this change:

in “public bool MoveNext()” I did add a test

    if (((attributes & 0x10) == 0) [b] && attributes != 15[/b])

I add this test about attributes != 15 because the iterator on my Panda will return a “fake” entry that has 15 as attributes (and 15 seems to me impossible based on the bitmask of System.IO.FileAttributes, of course) after the last real file. The only way to have a clean iteration on the folder is to discard the last entry it reports, and that code fix it.
Is this ok?

I also implemented a Dispose, to release the hanlders. Without this, and before to fix the test about attributes, the Panda reported the MaximumHanldedOpen after three calls on my directory browsing function (the close in "MoveNext was never called I think). So I think it’s better to have an explicit IDIsposable implementation that will release limited resource as file handles.

  // by baz
        #region Dispoable implementation

        private bool _disposed;

        public void Dispose()

            // Use SupressFinalize in case a subclass
            // of this type implements a finalizer.

        protected virtual void Dispose(bool disposing)
            // If you need thread safety, use a lock around these 
            // operations, as well as in your methods that use the resource.
            if (!_disposed)
                if (disposing)
                    if (_nativeFileFinderInstance != null)
                        _closeMethod.Invoke(_nativeFileFinderInstance, new object[] { });

                // Indicate that the instance has been disposed.              
                _nativeFileFinderInstance = null;
                _nativeFileInfoInstance = null;
                _disposed = true;


Thank you baz!

Your fixes look good to me. I will apply them put another revision on codeshare.

Thanks again!


I tried your code, but it fails @ _nativeFileFinderInstance = _nativeFileFinderConstructor.Invoke(new object[] { path, “*” });

Error msg = Exception System.IO.IOException - CLR_E_DIRECTORY_NOT_FOUND (1)

It works if the supplied path is @ the root for example path = “\SD\”, but it fails if the path points to sub directories under the root such as “\SD\hello\” or “\SD\Hello\2\”

Any thoughts?

I found a work around…

If you don’t include the \ at the end of the path it works, which is different from how the built in DirectoryInfo works.

For example (DirectoryInfoEx):

“\SD\Hello\” = BAD!
"\SD\Hello" = GOOD!

Thank you for the feedback I will update the code.

Thank you for making this contribution to the community…

This snippet was a life saver. You definitely deserve your Legend rank.

1 Like