Clearing Temporary Internet Files in C#

I recently ran in to an issue (I actually ran in to multiple issues*) when using the WebBrowser control in Visual Studio 2010, C# .Net Framework 4.0, whereby the embedded Internet Explorer browser was able to login to the website we were using for a project was not successfully tidying up after itself when closed.

After a bit of back and forth, trying to clear cookies, sessions and what-not I discovered that the web app was storing something essential in Temporary Internet Files. You may or may not be aware that when the WebBrowser control is closed it actually does not clear correctly cookies/sessions/temporary files – I don’t know the exact ins and outs of it as I was working to a deadline, but suffice to say it was an issue and one we needed solved.

I tried various bits of code and there was no clear answer, but eventually I did stumble upon one excellent bit of code which I’d just like to share:

/// <summary>
/// Clears the Internet Explorer cache folder (Temporary Internet Files)
/// </summary>
void clearIECache() {
    // Clear the special cache folder
    ClearFolder(new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.InternetCache)));

/// <summary>
/// Deletes all the files within the specified folder
/// </summary>
/// The folder from which we wish to delete all of the files
void ClearFolder(DirectoryInfo folder) {
    // Iterate each file
    foreach (FileInfo file in folder.GetFiles()) {
        try {
            // Delete the file, ignoring any exceptions
        } catch (Exception) {

    // For each folder in the specified folder
    foreach (DirectoryInfo subfolder in folder.GetDirectories()) {
        // Clear all the files in the folder

Which of course can be used by simply calling


-it does of course affect any other Internet Explorer browsing sessions that you may have open so use with caution, but ultimately it has proved perfect for our use (so far) and we will just have to accept it as is until we rewrite the application to replace the embedded browser.

I hope that comes in use to someone and is found with much more ease than what I went through!

*After finding out that we would be interfering with other browsing sessions I did try WebKit.Net and Open WebKit Sharp. Although I eventually successfully made use of WebKit.Net I found limitations in that SetAttribute() did not function on setting the value of input fields, and I was also unable to POST directly to a URL – to fix this I was then having to re-write part of the WebKit.Net source code but the limited documentation made this very difficult.

Open WebKit Sharp though seemingly more complete was even more difficult and I eventually deemed neither worth spending more time on. I did also look at .Net Gecko implementations but found these equally poorly documented and decided that to get the project completed in time I was better off working with the devil I knew – Internet Explorer (as much as it saddens me).

About Stephen Pickett

Stephen Pickett is a programmer, IT strategist and architect, project manager and business analyst, Oracle Service Cloud and telephony expert, information security specialist, all-round geek. He is currently Technical Director at Connect Assist, a social business that helps charities and public services improve quality, efficiency and customer engagement through the provision of helpline services and CRM systems.

Stephen is based in south Wales and attended Cardiff University to study Computer Science, in which he achieved a 2:1 grading. He has previously worked for Think Consulting Solutions, a leading voice on not-for-profit fundraising, Fujitsu Services and Sony Manufacturing UK as a software developer.

Stephen is the developer of ThinkTwit, a WordPress plugin that allows you to display multiple Twitter feeds within a blog.

1 thought on “Clearing Temporary Internet Files in C#

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.