tags:

views:

108

answers:

4

I am putting a check that if a directory by a name exists then it should delete that directory and replace it with the new one. For that i am having this code:

if (Directory.Exists(b))
{
    Directory.Delete(b);
    Directory.CreateDirectory(b);
}

where b is the name of the directory for which i am outting the check. I am getting a run time error that Directory is not empry, what shall i do?

+5  A: 

Try Directory.Delete(b, true)

trendl
See link: http://msdn.microsoft.com/en-us/library/system.io.directory.delete.aspx
Nathan Koop
+2  A: 

If the directory isn't empty you'll need to go in enumerate all the files and delete those first.

You'll also have to recurse down any subfolders too.

Or just call Directory.Delete(folder, true) of course!

ChrisF
See link: http://msdn.microsoft.com/en-us/library/system.io.directory.delete.aspx
Nathan Koop
@Nathan - What am I missing?
ChrisF
+2  A: 

You must call Directory.Delete(path, true) to force the deletion of subdirectories and files.

Daniel Brückner
+2  A: 

Delete subdirectories and files by setting a second boolean parameter to true:


if (Directory.Exists(b))
{
    Directory.Delete(b, true);
    Directory.CreateDirectory(b);
}
Ole Lynge