How to enum DirectoryEntries without foreach in C#?? 
Author Message
 How to enum DirectoryEntries without foreach in C#??

Hi All,

I can sucessfully loop through a list of DirectoryEntries with:

DirectoryEntry servers = new DirectoryEntry("IIS://x/W3SVC")
foreach(DirectoryEntry de in servers)
{do something}

but not with:

DirectoryEntry servers = new DirectoryEntry("IIS://x/W3SVC")
IEnumerator serverList = servers.Children.GetEnumerator();
serverList.Reset();
DirectoryEntry de = (DirectoryEntry) serverList.Current; ==>XX

This returns a "System.InvalidOperationException: Enumerator is positioned
before the first item or after the last item." in the XX marked line above.
I start with the same object and I am able to get an IEnumerator, but no
entries! I have to handle a large set of objects and wish to abort, if I've
found the right one.

Any hints, tips and thought what's wrong with my code/thinking would be very
welcomed!!!!

Best regards,
Manfred Braun

(Private)
Mannheim
Germany


(Remove the anti-spam-underscore to mail me!)



Mon, 20 Jun 2005 01:01:01 GMT  
 How to enum DirectoryEntries without foreach in C#??

Quote:
> DirectoryEntry servers = new DirectoryEntry("IIS://x/W3SVC")
> IEnumerator serverList = servers.Children.GetEnumerator();
> serverList.Reset();

if(serverList.MoveNext() == true) { /* do work */ }

The enumerator does not point to the first item until you move it. This
enables a more efficient loop:

while(e.MoveNext())
{
    // work

Quote:
}

---
Mickey Williams
Author "Microsoft Visual C# .NET", MS Press
www.servergeek.com


Mon, 20 Jun 2005 04:06:42 GMT  
 How to enum DirectoryEntries without foreach in C#??
Hello Mickey and All,

thanks a lot. I was too sloppy [???] to read the docs. I've done it
"carefully", but overseen the fact, that "Reset()" moves the pointer BEFORE
the list. Your tip was great!

Thanks and best regards,
Manfred Braun

(Private)
Mannheim
Germany


(Remove the anti-spam-underscore to mail me!)


Quote:


> > DirectoryEntry servers = new DirectoryEntry("IIS://x/W3SVC")
> > IEnumerator serverList = servers.Children.GetEnumerator();
> > serverList.Reset();

> if(serverList.MoveNext() == true) { /* do work */ }

> The enumerator does not point to the first item until you move it. This
> enables a more efficient loop:

> while(e.MoveNext())
> {
>     // work
> }

> ---
> Mickey Williams
> Author "Microsoft Visual C# .NET", MS Press
> www.servergeek.com



Mon, 20 Jun 2005 04:51:24 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. New to C# foreach

2. Hashtable problem using foreach loop - C#

3. better C# "foreach" request

4. using C# enum types in unmanged C++

5. Bitwise complement operator with [Flags] attribute on C# enum

6. C# Enum

7. using a C# uint Enum within a VB client

8. Enum the C# -vs- VB issue for all

9. using C# enum in C++ unmanaged class

10. typedef enum as subset of another enum

11. enum - enum ?

12. enum - enum ?

 

 
Powered by phpBB® Forum Software