开发者

Delete all files and folders in multiple directory but leave the directoy

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.Delet开发者_运维问答e();
}

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)


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);
  }
}


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);
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜