Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine number of files in a directory

Tags:

c#

.net

I need to determine the number of files/subdirectories in a directory. I don't care which files/directories are actually in that directory. Is there a more efficient way than using

_directoryInfo.GetDirectories().Length +
_directoryInfo.GetFiles().Length

Thanks.

like image 914
Mav3rick Avatar asked Aug 05 '09 20:08

Mav3rick


People also ask

How do I count the number of files in a folder in Windows 10?

From the folder Menu, From View tab. Select the Detail pane. On the right-hand side, the Details pane appears with the total number of items(files/ folders) within that folder.

How do you get files count in a directory in Linux?

The easiest way to count files in a directory on Linux is to use the “ls” command and pipe it with the “wc -l” command. The “wc” command is used on Linux in order to print the bytes, characters or newlines count. However, in this case, we are using this command to count the number of files in a directory.


2 Answers

That's probably about as good as it gets, but you should use GetFileSystemInfos() instead which will give you both files and directories:

_directoryInfo.GetFileSystemInfos().Length
like image 164
Sean Bright Avatar answered Sep 21 '22 23:09

Sean Bright


string[] filePaths = Directory.GetFiles(@"c:\MyDir\");

then just take the size of the filePaths array

code from: C#-Examples

like image 39
Tom Neyland Avatar answered Sep 21 '22 23:09

Tom Neyland