tags:

views:

255

answers:

2

well i like this nice 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)

+1  A: 

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

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

public void DeleteDirectoryFiles(DirectoryInfo dirInfo) {
    foreach(FileInfo files in dir.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);
  }
}
John Hartsock
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
A: 

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
am getting the "Using the generic type 'System.Action<T>' requires 1 type arguments" i am currently in Visual Studio express 2010 but my target framework is 2.0 if it helps any
NightsEVil
@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
but it needs to be a on click event like private void button5_Click(object sender, EventArgs e)
NightsEVil
@Nights... you can call the function from the button click event.
Anthony Pegram