I have a foreach loop that has try catch and finally. it goes through files in a folder and finally deletes them all….but now if i have two files, as it is deleting all( i need to delete all because of app functionality) it will not go though second file. is it possible to let foreach loop finish then delete all files in foreach method here is an example:
foreach (DirectoryInfo directory in directories)
{
foreach (FileInfo file in directory.GetFiles("*.csv"))
{
try
{
//do something with file
}
catch (Exception e)
{
//catch exception
}
finally
{
if (!IsFileLocked(file))
{
string[] files = Directory.GetFiles(@"C:\Test");
foreach (string filetoDelete in files)
{
File.Delete(filetoDelete);
}
}
}
}
}
hope it is clear enough.
I’m not very sure if I’m right understood what you’re asking for, but to me it seems solution pretty trivial, like this:
In other words, move file deletion from the second (nested)
foreachinto the new one.If this is not what you’re asking for, please clarify.