2

Well I like this nice piece of code right here it seems to work awesomely but I can't 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 as well like History and cookies and such how would I go about doing that (I would like to include at least 4-5 different folders)

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
NightsEVil
  • 507
  • 2
  • 13
  • 21

2 Answers2

1

Perhaps something like this would help. I did not test it.

public void DeleteDirectoryFolders(DirectoryInfo dirInfo){
    foreach (DirectoryInfo dirs in dirInfo.GetDirectories()) 
    { 
        dirs.Delete(true); 
    } 
}

public void DeleteDirectoryFiles(DirectoryInfo dirInfo) {
    foreach(FileInfo files in dirInfo.GetFiles()) 
    { 
        files.Delete(); 
    } 
}

public void DeleteDirectoryFilesAndFolders(string dirName) {
  DirectoryInfo dir = new DirectoryInfo(dirName); 
  DeleteDirectoryFiles(dir)
  DeleteDirectoryFolders(dir)
}

public void main() {
  List<string> DirectoriesToDelete;
  DirectoriesToDelete.add("c:\temp");
  DirectoriesToDelete.add("c:\temp1");
  DirectoriesToDelete.add("c:\temp2");
  DirectoriesToDelete.add("c:\temp3");
  foreach (string dirName in DirectoriesToDelete) {
    DeleteDirectoryFilesAndFolders(dirName);
  }
}
H. Mahida
  • 2,356
  • 1
  • 12
  • 23
John Hartsock
  • 85,422
  • 23
  • 131
  • 146
  • Something Like that but all under 1 button click event also it would be nice to have something in there to scan a whole drive and delete all of a specific extension that would be nice to. – NightsEVil Jun 02 '10 at 03:51
0

Here's a recursive function that will delete all files in a given directory and navigate down the directory structure. A pattern string can be supplied to only work with files of a given extension, as per your comment to another answer.

Action<string,string> fileDeleter = null;
fileDeleter = (directoryPath, pattern) =>
{
    string[] files;
    if (!string.IsNullOrEmpty(pattern))
        files = Directory.GetFiles(directoryPath, pattern);
    else
        files = Directory.GetFiles(directoryPath);

    foreach (string file in files)
    {
        File.Delete(file);
    }

    string[] directories = Directory.GetDirectories(directoryPath);
    foreach (string dir in directories)
        fileDeleter(dir, pattern);
};

string path = @"C:\some_folder\";
fileDeleter(path, "*.bmp");

Directories are otherwise left alone, and this can obviously be used with an array or list of strings to work with multiple initial directory paths.

Here is the same code rewritten as a standard function, also with the recursion as a parameter option.

public void DeleteFilesFromDirectory(string directoryPath, string pattern, bool includeSubdirectories)
{
    string[] files;
    if (!string.IsNullOrEmpty(pattern))
        files = Directory.GetFiles(directoryPath, pattern);
    else
        files = Directory.GetFiles(directoryPath);

    foreach (string file in files)
    {
        File.Delete(file);
    }

    if (includeSubdirectories)
    {
        string[] directories = Directory.GetDirectories(directoryPath);
        foreach (string dir in directories)
            DeleteFilesFromDirectory(dir, pattern, includeSubdirectories);
    }
}
Anthony Pegram
  • 123,721
  • 27
  • 225
  • 246
  • am getting the "Using the generic type 'System.Action' requires 1 type arguments" i am currently in Visual Studio express 2010 but my target framework is 2.0 if it helps any – NightsEVil Jun 02 '10 at 04:19
  • @NightsEVil, I believe the multiple argument Action is .NET 3.5+, but don't quote me on that. I'll add a standard function code sample. – Anthony Pegram Jun 02 '10 at 04:37
  • but it needs to be a on click event like private void button5_Click(object sender, EventArgs e) – NightsEVil Jun 02 '10 at 05:55
  • @Nights... you can call the function from the button click event. – Anthony Pegram Jun 02 '10 at 11:57