Click here to Skip to main content
15,901,373 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How do I make it so when my code hits an error which it does right now, it skips that file and starts with the next one, let me demo.


C#
private void Clear_Click(object sender, EventArgs e)
{



    string tempPath = Path.GetTempPath();
    DirectoryInfo di = new DirectoryInfo(tempPath);


    foreach (DirectoryInfo dir in di.GetDirectories())
    {
        dir.Delete(true);
    }

    foreach (FileInfo file in di.GetFiles())
    {
        try
        {
            file.Delete();
        }
        catch (Exception)
        {
            // Log error.
        }
    }

}










What it does now is it deleted files and folders in my temp folder but I want to make it so when it hits an error, when its not allowed to delete a file or a file is in use I dont want it to display an error I want it to skip that file and move on to the next one, what type of loop would I use for this ?

What I have tried:

I've looked over what types of loops would work but I couldnt find which one would fit me the best
Posted
Updated 6-May-16 9:49am

Put the filenames into a list.
Process the list with a for loop or for each loop.
Wrap the attempt to delete with a try-catch, when it fails make a note of why and which file, or update the list with that info.
The loop will automatically move onto the next file
 
Share this answer
 
Comments
BladeLogan 6-May-16 13:39pm    
Wont work since the temp files gain new folders everyday. I just added the continue; to the loop
CHill60 6-May-16 19:17pm    
Actually yes, it will work. How do I know this? Because this is a technique I've been using for the last 20 years. So much easier now with .NET. You create the list at the time you are trying to do these actions.. so instead of deleting the file after foreach (DirectoryInfo dir in di.GetDirectories()) you just add it to a list and then process that list.
BladeLogan 7-May-16 1:05am    
I am doing that.. In a way.. I have an extension list in a string with extensions such as *.rar, *.jpg etc etc
BillWoodruff 8-May-16 8:48am    
+5
Yep, you hit the wall of the hard fact that at any given moment a file's status could be changed. Some app opened it, some hardware change happened, permissions got changed, etc.

You could test the file for this, or that, and then, when you went to take action, the file has changed state.

There's an interesting article here on CP that tried to address this issue: [^].

But, even if you use those techniques, I think you still need a Try/Catch. Depending on what the overall goal is here, I'd think about generating summaries of the files that did not get deleted, with relevant error information.
 
Share this answer
 
You can use continue inside the catch:

C#
catch (Exception err)
{
   continue;
}
 
Share this answer
 
Comments
BladeLogan 6-May-16 13:46pm    
Thats what I am doing right now, it deletes files until it hits the error, it doesnt show an error message it just stops the action. Its kinda weird
BladeLogan 6-May-16 13:49pm    
Basically it doesnt restart the loop

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900