C# delete all files and folders in a directory but leave the directory

need help with code to delete all files and folders in a directory

Page 1 of 1

12 Replies - 18916 Views - Last Post: 02 June 2010 - 05:07 PM Rate Topic: -----

#1 NightsEvil  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 85
  • Joined: 23-May 10

C# delete all files and folders in a directory but leave the directory

Posted 01 June 2010 - 07:35 PM

well i like this peice of code right here it seems to work awsome but i cant seem to add any more directories to it

{
    DirectoryInfo dir = new DirectoryInfo(@"C:\temp"); 
       
        foreach(FileInfo files in dir.GetFiles())
        {
            files.Delete();
        }

        foreach (DirectoryInfo dirs in dir.GetDirectories())
        {
            dirs.Delete(true);
        }
    }



i would also like to add in special folders aswell like History and cookies and such how would i go about doing that (i would like to include atleast 4-5 different folders)

Is This A Good Question/Topic? 0
  • +

Replies To: C# delete all files and folders in a directory but leave the directory

#2 elbielefeld  Icon User is offline

  • D.I.C Head

Reputation: 70
  • View blog
  • Posts: 216
  • Joined: 18-May 10

Re: C# delete all files and folders in a directory but leave the directory

Posted 01 June 2010 - 07:42 PM

Create a List<DirectoryInfo>, add your items, use a foreach loop and your code.
Was This Post Helpful? 0
  • +
  • -

#3 NightsEvil  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 85
  • Joined: 23-May 10

Re: C# delete all files and folders in a directory but leave the directory

Posted 01 June 2010 - 09:13 PM

not sure if i understand can i get a example?
Was This Post Helpful? 0
  • +
  • -

#4 elbielefeld  Icon User is offline

  • D.I.C Head

Reputation: 70
  • View blog
  • Posts: 216
  • Joined: 18-May 10

Re: C# delete all files and folders in a directory but leave the directory

Posted 01 June 2010 - 09:36 PM

List<DirectoryInfo> foldersToClear = new List<DirectoryInfo>();
foldersToClear.add(new DirectoryInfo("Path1"));
foldersToClear.add(new DirectoryInfo("Path2"));

foreach (DirectoryInfo dir in foldersToClear)
{
//your code
}


Was This Post Helpful? 0
  • +
  • -

#5 danny_kay1710  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 28
  • View blog
  • Posts: 358
  • Joined: 27-April 08

Re: C# delete all files and folders in a directory but leave the directory

Posted 02 June 2010 - 01:04 AM

You will need to add the following line to use a list at the top of your code file with the other using statements using System.Collections.Generic for the above example to work
Was This Post Helpful? 0
  • +
  • -

#6 NightsEvil  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 85
  • Joined: 23-May 10

Re: C# delete all files and folders in a directory but leave the directory

Posted 02 June 2010 - 03:03 AM

so i tried this
        {
            List<DirectoryInfo> foldersToClear = new List<DirectoryInfo>();
            foldersToClear.Add(new DirectoryInfo("C:\temp"));
            foldersToClear.Add(new DirectoryInfo("C:\temp2"));
            List<FileInfo> FilesToClear = new List<FileInfo>();
            FilesToClear.Add(new FileInfo("C:\temp"));
            FilesToClear.Add(new FileInfo("C:\temp2"));

            foreach (FileInfo Files in FilesToClear)
                foreach (DirectoryInfo dir in foldersToClear)
                {
                    Files.Delete();
                    dir.Delete(true);

                }


but i get a illegal characters in path error, whys that? i even tried just this

        {
            List<DirectoryInfo> foldersToClear = new List<DirectoryInfo>();
            foldersToClear.Add(new DirectoryInfo("C:\temp"));
            foldersToClear.Add(new DirectoryInfo("C:\temp2"));

                foreach (DirectoryInfo dir in foldersToClear)
                {
                    
                    dir.Delete(true);

                }


and the same error and yes i have put the

using System.Collections.Generic;


at the top.. so whats wrong here?

This post has been edited by NightsEvil: 02 June 2010 - 03:04 AM

Was This Post Helpful? 0
  • +
  • -

#7 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: C# delete all files and folders in a directory but leave the directory

Posted 02 June 2010 - 05:11 AM

You need a recursive function...

private void DeleteFilesFromDirectory(string directoryPath)
{
    DirectoryInfo d = new DirectoryInfo(directoryPath);

    foreach (FileInfo fi in d.GetFiles())
    {
        fi.Delete();
    }

    foreach (DirectoryInfo di in d.GetDirectories())
    {
        DeleteFilesFromDirectory(di.FullName);

        di.Delete();
    }
}



and to call it...

DeleteFilesFromDirectory(@"C:\test");



This will delete all files and all directories inside of the parent directory("C:\test"), but will leave the parent directory("C:\test").

This post has been edited by eclipsed4utoo: 02 June 2010 - 05:13 AM

Was This Post Helpful? 0
  • +
  • -

#8 NightsEvil  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 85
  • Joined: 23-May 10

Re: C# delete all files and folders in a directory but leave the directory

Posted 02 June 2010 - 11:45 AM

what i dont get (and i have this problem alot) is how that can be a on click event? because

private void DeleteFilesFromDirectory(string directoryPath)



doesn't reference a button click
Was This Post Helpful? 0
  • +
  • -

#9 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5476
  • View blog
  • Posts: 11,762
  • Joined: 02-June 10

Re: C# delete all files and folders in a directory but leave the directory

Posted 02 June 2010 - 12:49 PM

Get in the habit of not putting your code directly in the button_click(object sender, eventargs e) handler.
That makes it hard to call from other methods.

Instead take the method as shown (that just takes the path to clear) and call *that* from your button click handler.

Now you can re-use the DeleteFilesFromDirectory method from any other place in your code much more easily.

button1_Click(object sender, eventargs e)
{
   DeleteFilesFromDirectory(textbox1.text);
}

button7_Click(object sender, eventargs e)
{
   DeleteFilesFromDirectory(UserDirectoryVariable);
}

button29_Click(object sender, eventargs e)
{
   DeleteFilesFromDirectory(TempDirectoryPath);
}

Was This Post Helpful? 2
  • +
  • -

#10 NightsEvil  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 85
  • Joined: 23-May 10

Re: C# delete all files and folders in a directory but leave the directory

Posted 02 June 2010 - 01:49 PM

got it to work PERFECTLY thank you very much for your explanation! BUT now i have a question on how i would do something like this

DeleteFilesFromDirectory(Environment.SpecialFolder.Cookies);


i know that doesnt work but id like to get it to work so i could put in special folder history, cookies, ect. (FIXED! GOT IT)

fixed it with
DeleteFilesFromDirectory(Environment.GetFolderPath(Environment.SpecialFolder.Cookies));


is there a way i can have say a message box pop up and say how files and how many kilobytes or megabytes (or even Gigabytes) were deleted?

This post has been edited by NightsEvil: 02 June 2010 - 02:02 PM

Was This Post Helpful? 0
  • +
  • -

#11 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5476
  • View blog
  • Posts: 11,762
  • Joined: 02-June 10

Re: C# delete all files and folders in a directory but leave the directory

Posted 02 June 2010 - 02:15 PM

Quote

is there a way i can have say a message box pop up and say how files and how many kilobytes or megabytes (or even Gigabytes) were deleted?

Yes there is.

You already have a recursive loop doing the delete. Set a couple counters before going into the loop. Have the loop add to the counters. Then display the total when done. The file size is part of the FileInfo object you are already calling.
Was This Post Helpful? 0
  • +
  • -

#12 elbielefeld  Icon User is offline

  • D.I.C Head

Reputation: 70
  • View blog
  • Posts: 216
  • Joined: 18-May 10

Re: C# delete all files and folders in a directory but leave the directory

Posted 02 June 2010 - 02:17 PM

The FileInfo class has a property named Length. This is how much bytes the file contains.
Before you delete a file, add this Length to a variable. Additionally, you should check, if the file could even be deleted (Try/Catch). After you have the final bytes, you can convert them to KB, MB or whatever you want.
You can count the deleted files by incrementing a variable each time a file is deleted.
Was This Post Helpful? 0
  • +
  • -

#13 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: C# delete all files and folders in a directory but leave the directory

Posted 02 June 2010 - 05:07 PM

There is a snippet in the C# snippets section that shows you how to get the total size of a folder including subfolders.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1