Deleting Internet Explorer Temporary Files in C#
Can anyone tell me why this wont work? I got the right folder path but it will not remove the files in that folder.
Code:
DirectoryInfo di = new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.InternetCache));
foreach (FileInfo fl in di.GetFiles())
{
fl.Delete();
}
Re: Deleting Internet Explorer Temporary Files in C#
Quote:
Originally Posted by 2MuchRiceMakesMeSick
Can anyone tell me why this wont work? I got the right folder path but it will not remove the files in that folder.
Code:
DirectoryInfo di = new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.InternetCache));
foreach (FileInfo fl in di.GetFiles())
{
fl.Delete();
}
By doesn't work, what do you mean, just not deleting or throwing exceptions?
Bill
Re: Deleting Internet Explorer Temporary Files in C#
it doesnt delete anything. no errors no exceptions
Re: Deleting Internet Explorer Temporary Files in C#
That folder requires some additional coding. Since index.dat amongst other files will not let you delete them, you have to take into account the access violations. There may be a quicker, cleaner method, but you can do this with recursive searching:
Code:
private void safeRecursiveDeleter(string path)
{
foreach (string FileName in Directory.GetFiles(path))
{
try
{
File.Delete(FileName);
}
catch { Exception ex; }//Certain files WILL give you access violations (index.dat)
}
foreach (string DirectoryName in Directory.GetDirectories(path))
{
safeRecursiveDeleter (DirectoryName);
try
{
Directory.Delete(DirectoryName, true); //Will kill the recursive search if there is no undeletable files
}
catch {Exception ex;}
}
}
//Called via:
safeRecursiveDeleter(Environment.GetFolderPath(Environment.SpecialFolder.InternetCache));
Bill
Re: Deleting Internet Explorer Temporary Files in C#
I don't have enough expertise in C# to play with InterOp and APIs yet. :(
I guess you can use the DeleteUrlCacheEntry API to do this. Here is a VB6 example. May be it woun't be hard to convert it in C#
Edit:
found a related page in MS Support pages. It may help ? http://support.microsoft.com/kb/326201/EN-US/