-4

Possible Duplicate:
Recursive delete of files and directories in C#

I am trying to delete all files and folders in a root folder but my program keeps crashing because some files are over the 256 or something limit.

What I need to do is go to the last folder delete all the files and then delete that folder and work my way upto the root folder and then delete that.

I'm stuck on how to do this please can someone help with a simple way to do this?

many thanks for any help

I'm using C#, the code is:

    private void RemoveDirectory(DirectoryInfo directory)
    {
        RemoveReadOnly(directory);

        directory.Delete(true);

        bool directoryExists = true;

        while (directoryExists)
            directoryExists = Directory.Exists(directory.FullName);

        SendProgressMessage(string.Format("Removed {0}", directory.FullName));
    }
Community
  • 1
  • 1
Sam Wrighton
  • 1
  • 1
  • 2
  • What is your code you use and exactly the error you have? – Peter Mar 15 '12 at 13:46
  • 3
    Post your code, post the exception details, post something that we can look at so we can start helping. See [Writing the perfect question](https://msmvps.com/blogs/jon_skeet/archive/2010/08/29/writing-the-perfect-question.aspx). – Oded Mar 15 '12 at 13:47
  • Similar Question: http://stackoverflow.com/questions/925192/recursive-delete-of-files-and-directories-in-c-sharp – Robar Mar 15 '12 at 13:47
  • @Robar: No, this is similar http://stackoverflow.com/q/2223007/284240 – Tim Schmelter Mar 15 '12 at 14:07
  • SamWrighton: This can be an infinite loop `while (directoryExists)directoryExists = Directory.Exists(directory.FullName);` since `Directory.Exists` will not change anything. – Tim Schmelter Mar 15 '12 at 14:09

3 Answers3

2

To delete your c:\temp directory:

Directory.Delete(@"c:\temp", true);

Is there a reason why you can't do this?

1

You should use the following function which goes around the shell api (if you error is that the path is longer as 256 chars):

[DllImport("kernel32.dll", CharSet=CharSet.Unicode, SetLastError=true)]
public static extern bool DeleteFile(string path);

And for directories:

[DllImport("kernel32.dll", CharSet=CharSet.Unicode, SetLastError=true)]
public static extern bool RemoveDirectory(string path);

And make sure you have the CharSet.Unicode, ansi will limit your path on 260 (MAX_PATH).

http://msdn.microsoft.com/en-us/library/aa363915%28VS.85%29.aspx

http://msdn.microsoft.com/en-us/library/aa365488%28v=vs.85%29.aspx

This functions can delete longer paths as 256 chars.

Peter
  • 27,590
  • 8
  • 64
  • 84
0

this is very similar to a tree sort etc.

you need a recursive function to get down to each of the leaf nodes (a folder without subfolders) and remove all files in that leaf and then the leaf itself.

basic pseudo code is :

function main()
{
    deleteNode(first node);
}

function deleteNode(node)
{
    foreach(child in node.children)
    {
        deleteNode(child);
        delete the child which should now be empty
    }

    delete all files in this node
}
Christopher Currens
  • 29,917
  • 5
  • 57
  • 77
spaceman
  • 1,628
  • 1
  • 16
  • 19