We help IT Professionals succeed at work.
Get Started

delete empty folders in a directory

wiggy353 asked
Last Modified: 2013-12-17
I am trying to delete all of the empty directories in a directory. I have code that works:

        private static void processDirectory(string startLocation)
            foreach (var directory in Directory.GetDirectories(startLocation))
                if (Directory.GetFiles(directory).Length == 0 && Directory.GetDirectories(directory).Length == 0)
                    Directory.Delete(directory, false);
                    log.Rows.Add(DateTime.Now, "Success", "The directory named " + directory + " was deleted because it was empty.");

Open in new window

which works good other than the line about adding the row to the log will not work because it is out of the context.

I tried putting this block of code into the main part of the code, but it will not work.


Watch Question
This problem has been solved!
Unlock 1 Answer and 5 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE