Jaqen H'ghar
Jaqen H'ghar

Reputation: 1879

How to clear IE cache without deleting files in use?

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 functions 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?

Upvotes: 1

Views: 1544

Answers (2)

Jack1987
Jack1987

Reputation: 727

Maybe using in your scripts using jquery this line of code to correct the browsers do caching:

$.ajaxSetup({ cache: false });

Upvotes: 0

stuartd
stuartd

Reputation: 73243

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.
        }
    }

Upvotes: 2

Related Questions