Did you try getting your foreach to simply return your directories as strings? Using something like http://msdn.microsoft.com/en-us/library/c1sez4sc.aspx .
Then you yourself handle the directories as needed? /Reuben Bartolo http://whatiseeinit.blogspot.com/ On 12 May 2012 08:42, Theraot <ther...@gmail.com> wrote: > According to MSDN the .NET 4 API to enumerate directories can leak > handles and I may need to explicitly call the garbage collector if I > want to delete directories or files after enumerating them (http:// > msdn.microsoft.com/en-us/library/dd997370.aspx). I hope this is not a > problem If I use foreach in C# because it should call Dispose which in > turn should release the handles. > > This problem seems to be particular of windows XP: > " > If you are running Windows XP or earlier, a delete operation on a file > or directory that follows an enumeration could fail if there is an > open handle that remains on one of the enumerated directories or > files. If this is occurring, you must induce a garbage collection to > remove the open handles. > " > And: > " > To remove open handles on enumerated directories or files > > Create a custom method (or function in Visual Basic) to contain your > enumeration code. > > Apply the MethodImplAttribute attribute with the NoInlining option to > the new method. For example: > > [MethodImplAttribute(MethodImplOptions.NoInlining)] > Private void Enumerate() > Include the following method calls, to run after your enumeration > code: > > The GC.Collect() method (no parameters). > > The GC.WaitForPendingFinalizers() method. > " > > I found a related question in stackoverflow: > > http://stackoverflow.com/questions/2663574/strange-thing-about-net-4-0-filesystem-enumeration-functionality > > Does anybody know if there is a way to avoid that leak? perhaps using > only TopDirectoryOnly instead of AllDirectories? Is it safe if I make > sure I call Dispose? Can anybody test it on Windows XP? > > I was suspecting that the leak is caused because the handles may have > been open before the enumerator is created, and thefore are kept open > by the enumerable. As it turns out the enumerator and the enumerable > are the same object, except if you call GetEnumerator in another > thread, in which case you get a clone. > > Note that this information has been removed for the documentation > of .NET 4.5, does that means that .NET 4.5 fixes the issue? > > Thanks. > > -- > You received this message because you are subscribed to the Google > Groups "DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML > Web Services,.NET Remoting" group. > To post to this group, send email to dotnetdevelopment@googlegroups.com > To unsubscribe from this group, send email to > dotnetdevelopment+unsubscr...@googlegroups.com > For more options, visit this group at > http://groups.google.com/group/dotnetdevelopment?hl=en?hl=en > or visit the group website at http://megasolutions.net > -- You received this message because you are subscribed to the Google Groups "DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting" group. To post to this group, send email to dotnetdevelopment@googlegroups.com To unsubscribe from this group, send email to dotnetdevelopment+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/dotnetdevelopment?hl=en?hl=en or visit the group website at http://megasolutions.net