Solved

How to check if a folder is empty

Posted on 2006-07-11
4
3,542 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB.NET HttpWebRequest 12 58
IO.FileSystemWatcher dies when used in Powershell 1 45
Code works but I need to redo it and assign values to Model 7 33
Server Error 11 47
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

867 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

15 Experts available now in Live!

Get 1:1 Help Now