How to remove huge list of folders using Parallel.For

  • Thread starter Thread starter Sudip_inn
  • Start date Start date
S

Sudip_inn

Guest
i have iterate in folder location and remove folder one by one....which taking time. i want to use Parallel.For to remove folders which speed up the operation.

my code

int totalDeletedFolders =0;
DataTable dt = GetFolders();

for (int r = 0; r <= dt.Rows.Count - 1; r++)
{
mainFolderPath = (dt.Rows[r]["DataLocation"] == DBNull.Value ? "" : dt.Rows[r]["DataLocation"].ToString());
if (mainFolderPath != "")
{
position = mainFolderPath.LastIndexOf("\\");
folderPath = mainFolderPath.Substring(0, position);
listOfFolders.Add(dt.Rows[r]["ID"].ToString());

if (Directory.Exists(folderPath))
{
RecursiveDelete(new DirectoryInfo(folderPath));
totalDeletedFolders++;
}
}

}

public static void RecursiveDelete(DirectoryInfo baseDir)
{
if (!baseDir.Exists)
return;

foreach (var dir in baseDir.EnumerateDirectories())
{
RecursiveDelete(dir);
}
var files = baseDir.GetFiles();
foreach (var file in files)
{
file.IsReadOnly = false;
file.Delete();
}
baseDir.Delete();
}

now guide me how to restructure the above code with parallel.For ?

also i am tracking no of main directories deleted by totalDeletedFolders++

so guide me how could i delete huge list of directories using Parallel.For and also track no of main directories deleted from parallel.For without using Lock() {} statement.thanks

Continue reading...
 
Back
Top