如何异步删除文件夹

时间:2012-01-20 22:00:06

标签: c# c#-4.0 asynchronous

我有一个运行的Windows服务,它会删除网络驱动器中的文件夹。我想删除异步。怎么办呢?

现在我正在遍历目录并调用

Directory.Delete(fullPath, true);

由于

2 个答案:

答案 0 :(得分:9)

我会使用任务并行库:

Task.Factory.StartNew(path => Directory.Delete((string)path, true), fullPath);

答案 1 :(得分:1)

如果您正在循环,则可以使用并行foreach


// assuming that you have a list string paths.  
// also assuming that it does not matter what order in which you delete them
Parallel.ForEach(theListOfDirectories, (x => Directory.Delete(x));