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.
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.
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.
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
string[] filePaths = Directory.GetFiles(@"c:\MyDir\");
then just take the size of the filePaths array
code from: C#-Examples
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With