I have 1 directory. In this directory it contains 9folders(subdirectory). Each folder(subdirectory) contains a variable number of folders(subsubdirectory), and also in (subsubdirectory) it contains some folder....so on.... I tried this method but i dont know how to expand it because of variable number of subdirectory.
public static long GetDirectoryNumberOfFiles(string directory, bool includeSubDir)
{
long numberFiles = Directory.GetFiles(@directory).Length;
string[] subfolderEntries = Directory.GetDirectories(directory);//result 9
//i tried this, but without luck.
var dirInfo =new DirectoryInfo(directory);
int dirCount = dirInfo.GetDirectories().Length;//result 9
if (includeSubDir)
{
for (int i = 0; i < subfolderEntries.Count(); i++)
{ numberFiles += Directory.GetFiles(subfolderEntries[i]).Length; }//result 38
}
return numberFiles;
}
When I right clicked the directory, it contains 23,026 files and 440folders. But with this method, i got 38files and 9folders. I know it just count the subdirectories but not all folders inside directory.