I'm creating a command-line utility that will delete sub-directories/files. If a file is in use, the System.IO.IOException
is thrown. I use a try-catch block within my for loop.
Question:
1.Is it bad practice to have a try-catch within a for loop?
2.If Yes, what is a better alternative?
My Code:
System.IO.DirectoryInfo di = new DirectoryInfo(path);
foreach (FileInfo file in di.GetFiles())
{
try
{
file.Delete();
}
catch(System.IO.IOException)
{
Console.WriteLine("Please Close the following File {0}", file.Name);
}
}