Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check whether a directory contains any files

 DirectoryInfo d = new DirectoryInfo(path);

 if()   // there is a file in the directory do something.

I can get the files if there exists any, but I also have to consider the possbility that there is no file inside that subfolder path.

like image 637
Cem Aytekin Avatar asked Dec 19 '25 00:12

Cem Aytekin


1 Answers

string[] files = System.IO.Directory.GetFiles(path);
if (files.Length == 0)
    Console.WriteLine("Empty");
else
    Console.WriteLine("Not Empty");

Using EnumerateFiles

 var fileCount = Directory.EnumerateFiles(@"C:\").Count();
 if (fileCount == 0)
    Console.WriteLine("Empty");
 else
    Console.WriteLine("Not Empty");
like image 177
Sajeetharan Avatar answered Dec 21 '25 13:12

Sajeetharan