I am writing a program in c#. Suppose I have some folders in Environment.CurrentDirectory. The folders are named after different numbers (say: 1,2,3 etc). Inside each folder there are some files. I want to count the total number of files inside a particular folder (say folder '1'), how will I do that?
Asked
Active
Viewed 180 times
0
-
http://stackoverflow.com/questions/11861151/find-all-files-in-a-folder – M.kazem Akhgary Oct 03 '15 at 14:30
-
i want to provide the folder name only and search in the current directory @Grant Winney I will not provide the full folder path – user5404530 Oct 03 '15 at 14:34
1 Answers
1
This should work. "path" contains the exact path to the wanted folder.
int fileCount = Directory.GetFiles(path).Count();
This is how you combine paths:
string path = Path.Combine(Environment.CurrentDirectory, "1");

Maor Veitsman
- 1,544
- 9
- 21
-
But i want to provide the folder name only and search in the current directory @Maor Veitsman – user5404530 Oct 03 '15 at 14:31
-
I hope I'm understanding you correctly. You can use Path.Combine to create a proper full path. I edited my answer with a sample line of code. – Maor Veitsman Oct 03 '15 at 14:34
-
'System.Array' does not contain a definition for 'Count' and no extension method 'Count' accepting a first argument of type 'System.Array' could be found (are you missing a using directive or an assembly reference?)---getting this error @Maor Veitsman – user5404530 Oct 03 '15 at 14:41
-