Solved

"Directory is not empty" error message when deleting directory

Posted on 2010-11-17
1
469 Views
Last Modified: 2012-06-21
I have a simple function for creating a directory, check first to see if the directory already exists.  If it does, delete it.

I keep getting the error message "the directory is not empty", but I specify that the recursive parameter is TRUE, which should delete these subdirectories/files...

Am I missing something?

 
protected void CreateDirectory(string target_directory)
    {
        try
        {
            if (Directory.Exists(target_directory))
            {
                try
                {
                    lblOutput.Text += "<br />deleting directory: " + target_directory;
                    Directory.Delete(target_directory, true);
                }
                catch (Exception Ex)
                {
                    Session["error_message"] += "<br />Error deleting " + target_directory + ": " + Ex.Message;
                    throw (Ex);
                }
            }

            try
            {
                Directory.CreateDirectory(target_directory);
            }
            catch (Exception Ex)
            {
                Session["error_message"] += "<br />Error creating directory (" + target_directory + "): " + Ex.Message;
            }
        }
        catch (Exception Ex)
        {
            Session["error_message"] += "<br />Error during CreateDirectory(" + target_directory + "): " + Ex.Message;
            throw (Ex);
        }
    }

Open in new window

0
Comment
Question by:quipuha
1 Comment
 
LVL 30

Accepted Solution

by:
Reza Rad earned 500 total points
Comment Utility
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

743 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

10 Experts available now in Live!

Get 1:1 Help Now