How to delete Internet Explorer's temporary files and cookies with C#

by Matthias Broschk October 14, 2008 01:59

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.

Tags: , , , ,

C# | Internet Explorer

Comments

2/19/2010 5:36:15 PM #

Ivan

Thank you so much!

Ivan Croatia

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading



Powered by BlogEngine.NET 1.5.0.7
Theme by Mads Kristensen | Modified by Mooglegiant

About the author

Matthias Broschk from Hamburg (Germany) wrote his first goto statements in QuickBasic at the age of fifteen, switched to Visual Studio (i.e. Visual Basic / Visual C++) at version 5.0 and has been an addict of .NET since its early beginnings. There have been many other languages and frameworks (Java, PHP, ... ), but none about which he has been as enthusiastic as .NET. These days you can find more information in blogs rather than anywhere else. Therefore he has decided to share his experiences and start yet another .NET blog.