tags:

views:

26

answers:

1

I am uploading image files in my mvc web app. I want to show this as logo on my site whenever I upload any selected image. For this, i need to clear the browser cache. Following is set of funtions I am using for clearing cache.

void clearIECache()
    {
      ClearFolder(new DirectoryInfo(Environment.GetFolderPath
         (Environment.SpecialFolder.InternetCache)));
    }

    void ClearFolder(DirectoryInfo folder)
    {
      foreach (FileInfo file in folder.GetFiles())
      { file.Delete(); }
      foreach (DirectoryInfo subfolder in folder.GetDirectories())
      { ClearFolder(subfolder); }
    }

Problem with above code is that it interrupts whenever it tries to delete any file that's in use. I want to skip such files sothat it can work smoothly. Is there any way to achieve this?

thanks, kapil

+2  A: 

Use Try/Catch to trap the errors and ignore them:

    foreach (FileInfo file in folder.GetFiles())
    {
        try
        {
            file.Delete();
        }
        catch (Exception)
        {
           // Swallow "File in use" errors.
        }
    }
Stuart Dunkeld
If you do this, make sure you always include a comment in the empty catch block, explaining to the programmer that follows you why the empty catch block is there.
Robert Harvey