diff --git a/sources/OpenMcdf/CFStorage.cs b/sources/OpenMcdf/CFStorage.cs index 0a238fc7..fa2b3908 100644 --- a/sources/OpenMcdf/CFStorage.cs +++ b/sources/OpenMcdf/CFStorage.cs @@ -423,34 +423,23 @@ public void VisitEntries(Action action, bool recursive) { CheckDisposed(); - if (action != null) - { - List subStorages - = new List(); - - Action internalAction = - delegate (IRBNode targetNode) - { - IDirectoryEntry d = targetNode as IDirectoryEntry; - if (d.StgType == StgType.StgStream) - action(new CFStream(CompoundFile, d)); - else - action(new CFStorage(CompoundFile, d)); + if (action is null) + return; // TODO: Reorder and throw ArgumentNullException in v3 - if (d.Child != DirectoryEntry.NOSTREAM) - subStorages.Add(targetNode); + Stack stack = new(); + stack.Push(this); - return; - }; - - Children.VisitTree(internalAction); - - if (recursive && subStorages.Count > 0) + while (stack.Count > 0) + { + CFItem current = stack.Pop(); + if (current is CFStorage storage) { - foreach (IRBNode n in subStorages) + foreach (IDirectoryEntry de in storage.Children.Cast()) { - IDirectoryEntry d = n as IDirectoryEntry; - new CFStorage(CompoundFile, d).VisitEntries(action, recursive); + CFItem item = de.StgType == StgType.StgStream ? new CFStream(CompoundFile, de) : new CFStorage(CompoundFile, de); + action(item); + if (recursive) + stack.Push(item); } } }