I’m not sure, how to free DirectoryEntry object, when I worked with it’s children.
Do I need to free all childs aтd then free parent or just free parent?
For example,
using(DirectoryEntry entry = new DirectoryEntry())
{
foreach (DirectoryEntry childEntry in entry.Children)
{
....
}
}
Is it enough?
Or need additional code such as
using(DirectoryEntry entry = new DirectoryEntry())
{
foreach (DirectoryEntry childEntry in entry.Children)
{
using(childEntry)
{
...
}
}
}
?
You have to free every enumerated child.
Moreover, you should note, that
DirectoryEntry.Childrenreturns newly created object every time you access its getter. This is disgustingly, because it is a rough violation of MS own guidelines (DirectoryEntry.Childrenmust be a method), but it is true.So: