0

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?

user5404530
  • 27
  • 1
  • 10

1 Answers1

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
  • Add "using System.Linq;" in the beginning of your file. – Maor Veitsman Oct 03 '15 at 14:44