If your application uses the WebBrowser control, due to privacy/security concerns you might want to clean up the browser cache when the application terminates. The IE's temporary files are stored in a special sub-directory in your "Documents and Settings\[Username]\Local Settings' directory called Temporary Internet Files. Although this looks like a normal directory (when opened with Windows Explorer), it actually consists of a number of sub-directories with random names with a file "index.dat" as its indexing database.
The most obvious way would be delete the sub-directories and files recursively. Unfortunately "index.dat" would still contain a list of all visited sites.
void ClearFolder(DirectoryInfo folder)
{
foreach (FileInfo file in folder.GetFiles())
{
try
{
file.Delete();
}
catch{}
}
foreach (DirectoryInfo subfolder in folder.GetDirectories())
{
ClearFolder(subfolder);
}
}
ClearFolder(new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.InternetCache)));
An alternative that I found on the Microsoft Support site accesses the "WinInet.dll" API . The full source code is available and because it is quite large (nearly 160 lines of code) I won't publish it here.
I experienced it to be rather slow and kept searching for better alternative and found found the following undocumented solution.
System.Diagnostics.Process.Start("rundll32.exe", "InetCpl.cpl,ClearMyTracksByProcess 8"); // Clear Temporary Files
System.Diagnostics.Process.Start("rundll32.exe", "InetCpl.cpl,ClearMyTracksByProcess 2"); // Clear Cookies
Here is a full list of possible parameters:
- Temporary Internet Files
ClearMyTracksByProcess 8
- Cookies
ClearMyTracksByProcess 2
- History
ClearMyTracksByProcess 1
- Form Data
ClearMyTracksByProcess 16
- Passwords
ClearMyTracksByProcess 32
- Delete All
ClearMyTracksByProcess 255
- Delete All – “Also delete files and settings stored by add-ons”
ClearMyTracksByProcess 4351
It is even possible to combine two or more by adding the numbers ( "ClearMyTracksByProcess 10" would clear both Cookies [2] and Temporary Internet Files [10] ). During the deletion process the same progress bar box is shown, that you can see when you delete the files manually in IE's Internet options. Unfortunately this box can't be turned of. Another disadvantage is that these functions are undocumented and therefore subject to change with every new version of Internet Explorer.
There isn't really the best solution for the problem on how to remove the Temporary Internet Files and it depends on the application and it's individual requirements which alternative to choose.
Source code is available here.