Solved

"Directory is not empty" error message when deleting directory

Posted on 2010-11-17
1
477 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
ID: 34158316
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
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…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

813 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