How to read or write millions of directories?

I have a program that will save about 3 million directories, and read them or write to them, but when I want to save them using Directory.GetDirectories(SourcEDirectorY.Text, "*", SearchOption.AllDirectories), it will not respond.

What should I do to solve this problem?

+3
source share
1 answer

Instead, Directory.GetDirectoriescall Directory.EnumerateDirectories .

Who cares? EnumerateDirectoriesreturns IEnumerable<string>, whereas GetDirectoriesreturns an array of strings: string[].

So if you do this:

foreach (var dir in Directory.GetDirectories(...))
{
}

Then you wait until the entire array of entries in the directory is read from disk. Compare this to EnumerateDirectories:

foreach (var dir in Directory.EnumerateDirectories(...))
{
}

, . . , , , , .

, . , , . , , . , , , . , , , - .

+5
source

All Articles