简体   繁体   中英

How can I count the number of elements in IEnumerator?

I am getting all files in a directory by following code.

IEnumerator FILES = Directory.GetFiles(
                DIRECTORY_PATH).GetEnumerator();

How can I get the total number of files? There's no FILES.Count();

Directory.GetFiles(@"C:\yourdir").Length

直接给你数

First you can get your files string[] , count the numbers in it, then get your enumerator:

string[] files = Directory.GetFiles(DIRECTORY_PATH);
int count = files.Length;
IEnumerator enumerator = files.GetEnumerator();

If you really want to stick with " GetEnumerator() " ...

IEnumerator files = Directory.GetFiles(DIRECTORY_PATH).GetEnumerator();
int count = 0;
while (files.MoveNext())
{
    count++;
}

// after this loop you will have total files count in count varibale.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM