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

Deleting IE Cache
jen0s
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())
foreach (System.IO.DirectoryInfo subfolder in folder.GetDirectories()){
try
{
file.Delete();
}
catch (Exception ex)
{
MessageBox.Show("Unable to delete file: " + file.FullName + "\n\n" + ex.Message);
}
}
{
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)
sj2509
amselem
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();
}
}
}
=================================================================
blackpenny15
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....
waldedg
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
DaveC#
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();
}
}
}
Douwe
Do u know what's happening
LostGamer
What can be
gorance
I have JPG, HTM, HTML, JS.. all that in the folder !
hmm, index.dat is at "\Temporary Internet Files\Content.IE5"
Michael Mac
It should work.
Can you post the exception message.
Denton
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.
Waleed Seada
Mary Lindholm
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.
Dogbrain
hi.
Is your problem solved
Thank you,
Bhanu.