Link to home
Start Free TrialLog in
Avatar of quipuha
quipuha

asked on

"Directory is not empty" error message when deleting directory

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

ASKER CERTIFIED SOLUTION
Avatar of Reza Rad
Reza Rad
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial