Deleting IE Cachea

Well, i'm trying to delete the IE Cache with this:

#region IECache
private void IECache()
{

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);

}
#endregion IECache

but it doesn't work, cz i got a message telling that "index.dat" is being used by the system, then how could i delete everything but not index.dat

and how can i put my code colered in posts :p


Answer this question

Deleting IE Cachea

  • Roman T

    It should work.

    Can you post the exception message.



  • Robert Wafle

    The first box show 1, while in the second 0

  • barf

    Then what should i do I have seen my temporary files folder and files still there.

  • kop48

    I was able to delete all the temporary IE cache files. I used the following codes as shown below:

     

    =================================================================

    using System;< xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

    using System.Collections.Generic;

    using System.Text;

    using System.IO;

     

    namespace DeleteIECache

    {

        class Program

        {

            public void DeleteCache()

            {

                DirectoryInfo directory = new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.InternetCache));

                CleanUp(directory);

            }

     

            private void CleanUp(DirectoryInfo directory)

            {

                Console.WriteLine("Deleting files under " + directory.FullName);

     

                FileInfo[] files = directory.GetFiles();

                foreach (FileInfo file in files)

                {

                    try

                    {

                        file.Delete();

                        Console.WriteLine(file.FullName + " Deleted successfully");

                    }

                    catch (Exception)

                    {

                        Console.WriteLine("Unable to delete :" + file.FullName);

                    }

                }

     

                DirectoryInfo[] subDirectories = directory.GetDirectories();

                foreach (DirectoryInfo subDirectory in subDirectories)

                {

                    CleanUp(subDirectory);

                    if (subDirectory.GetFiles().Length == 0)

                    {

                        subDirectory.Delete();  

                    }

                }

               

            }

           

            static void < xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" />Main(string[] args)

            {

                Program p = new Program();

                p.DeleteCache();

            }

        }

    }

    =================================================================



  • Wimmo

    I don't really know. Seeing as I can't replicate your problem it makes it harder to help

    Oh, and this:

    4) Have another look at the temporary internet files location (may require refresh of directory contents). The files will be gone.

    Should be exit the directory (i.e. go up one level in the hierarchy) and then go back in the directory. Somehow doing a refresh of the contents doesn't work (for me anyway).

    Are message boxes only appearing for index.dat or are you getting then for other files in your temporary internet folder as well

    What other contents (apart from index.dat) appear in your temporary internet files folder


  • quadratron

    OK, before the first foreach loop put the following:

    System.IO.FileInfo[] x = folder.GetFiles();
    MessageBox.Show(x.Length.ToString());

    And after the last foreach loop put the following:

    System.IO.FileInfo[] y = folder.GetFiles();
    MessageBox.Show(y.Length.ToString());

    Tell us what the message boxes show....


  • xinpis

    It's not deleting files in "\Temporary Internet Files" but appeared a message telling that can't remove index.dat !
    What can be

  • Laird

    hi.

    Is your problem solved

    Thank you,
    Bhanu.



  • bakusek

    Hi there,

    If you handle the execptions that get thrown when you can't delete a file, your code will proceed along it's merry way and basically the files that can't be deleted (i.e. those that threw exceptions) will be ignored.

    For example:


    System.IO.DirectoryInfo folder = new System.IO.DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.InternetCache));

    foreach(System.IO.FileInfo file in folder.GetFiles())
    {
        try
        {
            file.Delete();
        }
        catch (Exception ex)
        {
            MessageBox.Show("Unable to delete file: " + file.FullName + "\n\n" + ex.Message);
        }
    }

    foreach (System.IO.DirectoryInfo subfolder in folder.GetDirectories())
    {

        try

        {

            subfolder.Delete(true); //Delete all subfiles & subfolders

        }

        catch (Exception ex)

        {

            MessageBox.Show("Unable to delete folder: " + subfolder.FullName + "\n\n" + ex.Message);

        }

    }


    Hope that helps a bit, but sorry if it doesn't.

    PS. I managed to get code colouring by copying from the IDE into MS Word and then from MS Word into the forum's interface. I got this tip from another post somewhere on the MSDN forums but I don't remember where (props to whoever posted it though)


  • JimKillian

    Well, it deleted everything from "\Temporary Internet Folder\Content.IE5", but nothing from "\Temporary Internet Folder"

    Do u know what's happening

  • bluffmastor

    The code is meant to display a message box when it can't delete something but will otherwise progress and delete everything it can delete. That makes it different from the code you have right now as it will have unhandled exceptions and bomb out when it finds something it can't delete.

    I have tried this on my machine and it deletes everything it can in Temporary Internet Files (apart from things it can't delete....then it will display a message box).

    Here's something you can do to check (might be easier if you don't have a web browser open, but it doesn't matter)

    1) Go to your temporary internet files location as specified by
    Environment.SpecialFolder.InternetCache. This will generally be under C:\Documents And Settings\<Your User Name>\Local Settings\Temporary Internet Files

    2) Bung a few test files in there (copy from some other location if you have to)

    3) Run your code

    4) Have another look at the temporary internet files location (may require refresh of directory contents). The files will be gone.


  • regina

    I tried with this but it didn`t work :(

    using System;

    using System.Collections.Generic;

    using System.ComponentModel;

    using System.Data;

    using System.Drawing;

    using System.Text;

    using System.Windows.Forms;

    using System.IO;

    using Microsoft.Win32;

    namespace Pc_Cleaner

    {

    public partial class Form1 : Form

    {

    public Form1()

    {

    InitializeComponent();

    }

    #region IECache

    private void IECache()

    {

    DirectoryInfo directory = new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.InternetCache));

    CleanUp(directory);

    }

    private void CleanUp(DirectoryInfo directory)

    {

    Console.WriteLine("Deleting files under " + directory.FullName);

    FileInfo[] files = directory.GetFiles();

    foreach (FileInfo file in files)

    {

    try

    {

    file.Delete();

    Console.WriteLine(file.FullName + " Deleted successfully");

    }

    catch (Exception)

    {

    Console.WriteLine("Unable to delete :" + file.FullName);

    }

    }

    DirectoryInfo[] subDirectories = directory.GetDirectories();

    foreach (DirectoryInfo subDirectory in subDirectories)

    {

    CleanUp(subDirectory);

    if (subDirectory.GetFiles().Length == 0)

    {

    subDirectory.Delete();

    }

    }

    }

    #endregion IECache

    private void pictureBox2_Click(object sender, EventArgs e)

    {

    Form1 p = new Form1();

    p.IECache();

    }

    }

    }


  • Doug Mitcham

    It is reading the temporary internet folder from the environment variable. XP maintains a separate profile for separate logged in user.

    To execute your application, use admin profile.

     



  • Zarryn

    Weel i seen it again and files still there !

    I have JPG, HTM, HTML, JS.. all that in the folder !

    hmm, index.dat is at "\Temporary Internet Files\Content.IE5"

  • Deleting IE Cachea