Solved

How to check if a folder is empty

Posted on 2006-07-11
4
3,549 Views
Last Modified: 2011-02-04
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!
0
Comment
Question by:boukaka
  • 2
4 Comments
 
LVL 18

Accepted Solution

by:
Ravi Singh earned 250 total points
ID: 17082941
using System.IO;

...

string folderPath = @"C:\Temp";

if (Directory.GetFiles(folderPath).Length == 0)
{
      MessageBox.Show(folderPath + " is empty");
}
0
 
LVL 18

Expert Comment

by:Ravi Singh
ID: 17083006
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
 

Author Comment

by:boukaka
ID: 17083355
Awesome, thank you!
0
 

Expert Comment

by:sqlDude170
ID: 34816384
Excellent example...perfect
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now