How to check if a folder is empty

Is there a way to check if a folder is empty and putting up a messagebox if it is? I will not know the names of the files that will be in the folder.
Thanks!
boukakaAsked:
Who is Participating?
 
Ravi SinghConnect With a Mentor Senior Software EngineerCommented:
using System.IO;

...

string folderPath = @"C:\Temp";

if (Directory.GetFiles(folderPath).Length == 0)
{
      MessageBox.Show(folderPath + " is empty");
}
0
 
Ravi SinghSenior Software EngineerCommented:
if the folder may have subdirectories, you can check that by calling GetDirectories.

string folderPath = @"C:\Temp";

if (Directory.GetDirectories(folderPath).Length == 0 && Directory.GetFiles(folderPath).Length == 0)
{
      MessageBox.Show(folderPath + " is empty");
}
0
 
boukakaAuthor Commented:
Awesome, thank you!
0
 
sqlDude170Commented:
Excellent example...perfect
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.