-1

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();

3 Answers3

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

will give you count directly

Liquid Core
  • 1
  • 6
  • 27
  • 52
  • Fixed. To much used to Linq that I forgot about return type – Liquid Core Apr 17 '19 at 11:33
  • 1
    @GiladGreen it's true that there's no 'need' for 'System.Linq' here. Anyway, since an array is an `ICollection`, a LInQ `Count()` [is optimized to](https://referencesource.microsoft.com/System.Core/R/41ef9e39e54d0d0b.html) directly access the `Count` property of the `ICollection`; which in turn [returns the `Length` property of the array](https://referencesource.microsoft.com/mscorlib/system/array.cs.html#680). – Aly Elhaddad Apr 17 '19 at 11:40
1

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();
SᴇM
  • 7,024
  • 3
  • 24
  • 41
0

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.
Yawar Murtaza
  • 3,655
  • 5
  • 34
  • 40