We know all the internet cache files are stored in “Temporary Internet Files” folder. So, to clear Internet Cache, we all have to do is delete all the files that are present in that folder. In this article I am showing you a quick example, how you can clear Internet Cache using C#.
Instructions:
- As we have to clear all the files present in “Temporary Internet Files” folder and also in all the subfolders, So we have to call a function which will receive Folder name as one parameter, and we write code to loop through each of the file present in that folder and delete that. Now following is the syntax to call that function. Just place this one line of code where you want to clear Internet Cache. e.g If you want to clear Internet Cache on a button click, then place this line of code in the button_click event for that button.
EmptyCache(new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.InternetCache)));
-
Here is the code for that function which we are calling to delete all the files present in “Temporary Internet Files” folder and its subfolders
private void EmptyCache(DirectoryInfo folder)
{
foreach (FileInfo file in folder.GetFiles())
{
//loop through each file in the folder and delete that
try
{
file.Delete();
}
catch (Exception)
{ //we have to use try cache, because there are some files that can’t be delete
// because those are system files.
// So we don’t want to stop our program at that point when our program try
// to delete any of those file and exception occurs
}
}
//loop through all the sub folder in the current folder provided
foreach (DirectoryInfo subfolder in folder.GetDirectories())
{
EmptyCache(subfolder);
}
}
Here is screenshot of my Temporary Internet Files Folder just before running the code to delete or clear the Internet Cache
Now here is screenshot my Temporary Internet Files folder after running the code ![]()
Incoming search terms:
- c# ie cache (2)
- Clear cahe delete (1)
- clear image cache c# (1)
- registry cache windows 7 c# (1)
- vbnet delete win7 temporary internet (1)
- 윈도우즈7 temporary internet files delete source (1)