Delete Temporary Internet Files

In IE5, you can delete Temporary Internet Files in "Internet Options" property sheet. Do you ever wonder how to delete Temporary Internet Files in your application? Here is how it can be done by using WinInet APIs: FindFirstUrlCacheEntry, FindNextUrlCacheEntry, DeleteUrlCacheEntry, and FindCloseUrlCache.

It is not documented, but I have tested it and it works, with VC++ 6.0 on Win98/NT.


#include <wininet.h>
//
// Delete all files in the Temporary Internet Files folder
// 
// Note that you can specify what NOT to delete by testing entry type
// In code below, cookie entries are not deleted
// [see if (!(lpCacheEntry->CacheEntryType & COOKIE_CACHE_ENTRY))]
// 
BOOL DelTempFiles() 
{
    BOOL bResult = FALSE;
    BOOL bDone = FALSE;
    LPINTERNET_CACHE_ENTRY_INFO lpCacheEntry = NULL;  
 
    DWORD  dwTrySize, dwEntrySize = 4096; // start buffer size    
    HANDLE hCacheDir = NULL;    
    DWORD  dwError = ERROR_INSUFFICIENT_BUFFER;
    
    do 
    {                               
        switch (dwError)
        {
            // need a bigger buffer
            case ERROR_INSUFFICIENT_BUFFER: 
                delete [] lpCacheEntry;            
                lpCacheEntry = (LPINTERNET_CACHE_ENTRY_INFO) new char[dwEntrySize];
                lpCacheEntry->dwStructSize = dwEntrySize;
                dwTrySize = dwEntrySize;
                BOOL bSuccess;
                if (hCacheDir == NULL)                
                  
                    bSuccess = (hCacheDir 
                      = FindFirstUrlCacheEntry(NULL, lpCacheEntry,
                      &dwTrySize)) != NULL;
                else
                    bSuccess = FindNextUrlCacheEntry(hCacheDir, lpCacheEntry, &dwTrySize);

                if (bSuccess)
                    dwError = ERROR_SUCCESS;    
                else
                {
                    dwError = GetLastError();
                    dwEntrySize = dwTrySize; // use new size returned
                }
                break;

             // we are done
            case ERROR_NO_MORE_ITEMS:
                bDone = TRUE;
                bResult = TRUE;                
                break;

             // we have got an entry
            case ERROR_SUCCESS:                       
        
                // don't delete cookie entry
                if (!(lpCacheEntry->CacheEntryType & COOKIE_CACHE_ENTRY))                
                   
                 DeleteUrlCacheEntry(lpCacheEntry->lpszSourceUrlName);
                    
                // get ready for next entry
                dwTrySize = dwEntrySize;
                if (FindNextUrlCacheEntry(hCacheDir, lpCacheEntry, &dwTrySize))
                    dwError = ERROR_SUCCESS;          
     
                else
                {
                    dwError = GetLastError();
                    dwEntrySize = dwTrySize; // use new size returned
                }                    
                break;

            // unknown error
            default:
                bDone = TRUE;                
                break;
        }

        if (bDone)
        {   
            delete [] lpCacheEntry; 
            if (hCacheDir)
                FindCloseUrlCache(hCacheDir);         
                                  
        }
    } while (!bDone);
    return bResult;
}


Comments

  • Solution to Delete Temporary Internet Files in IE 6

    Posted by divesh4u on 06/09/2006 07:25am

    You can do this by following steps:
    
    You can type simpely in header 
    
    #pragma comment(lib, "wininet.lib") 
    
    --------OR---------
    
    In the your Project in VC++ 6.0
    Go to Project -> Settings -> Links
    
    In the Object/Library Modules type: Wininet.lib
    
    It will definately work.
    
    Divesh K. Jain

    • How does it work?

      Posted by WorldNet on 10/03/2006 03:53pm

      Are you going to use a NOTEPAD to use this code? Thanks

      Reply
    Reply
  • This doesn't work .... :-(

    Posted by acroitoriu on 04/24/2005 04:35pm

    I tried to use your code in order to read the content of a dynamic web page (written in PHP), which always has a different content. I used your Clear Histoy code and it works, but this function doesn't work... I get the following erros: --------------------Configuration: HS_VC - Win32 Debug-------------------- Compiling... HS_VCDlg.cpp Linking... HS_VCDlg.obj : error LNK2001: unresolved external symbol __imp__FindCloseUrlCache@4 HS_VCDlg.obj : error LNK2001: unresolved external symbol __imp__DeleteUrlCacheEntry@4 HS_VCDlg.obj : error LNK2001: unresolved external symbol __imp__FindNextUrlCacheEntryA@12 HS_VCDlg.obj : error LNK2001: unresolved external symbol __imp__FindFirstUrlCacheEntryA@12 Debug/HS_VC.exe : fatal error LNK1120: 4 unresolved externals Error executing link.exe. HS_VC.exe - 5 error(s), 0 warning(s) ---------------------------------------- I'm using IE 6.0 ... under Windows XP with VC++ 6.0. This code is only for IE5??? Why it doesn't work for me ??? It's really strange...

    • Solution

      Posted by divesh4u on 06/09/2006 07:34am

      Look at my Topic Solution To Delete Temporary Internet Files in IE 6

      Reply
    • My code is also not working

      Posted by Djain on 06/08/2006 09:02am

      I'm also using IE 6.0. it not working for me. Please provide the solution for IE 6.0.

      Reply
    Reply
  • how to delete cookie and temporary internet files using java

    Posted by Legacy on 02/12/2004 12:00am

    Originally posted by: sami

    how to delete cookie and temporary internet files using java
    
    send me mail ..

    i'm doing project

    Reply
  • How to recognise the file extension of cached items in the Internet Temporary Files folder ?

    Posted by Legacy on 01/15/2004 12:00am

    Originally posted by: Brent

    HI, there
    
    

    I am making a programme that is expected to be able to fetch the pictures files in the Internet Temporary Files folder, such as .jpg and .gif etc. My thoughts is to use winInet API to recognise each cacheEntry's extension, if it is .jpg or .gif then do the further manipulation.

    The CacheEntry variable is definded as:
    LPINTERNET_CACHE_ENTRY_INFO lpCacheEntry = NULL;

    In a loop, when each item is fetched by FindNextUrlCacheEntry , and then I wish to use lpCacheEntry -> lpszFileExtension to find out the extension of the cached entry. The problem is lpCacheEntry -> lpszFileExtension seems cannot contain some extention like "jpg" or "gif", (but I found no prolem with "htm" or "css" etc.)so I cannot use something like:
    if (lpCacheEntry -> lpszFileExtension is "jpg") then do....

    the date type of lpszFileExtension is LPTSTR.

    Does anyone can help me, please. Thank you

    Reply
  • How To Recover The The Deleted URL Cache .......?

    Posted by Legacy on 01/12/2004 12:00am

    Originally posted by: Nilesh


    Help...How To Recover The The Deleted URL Cache .......?

    Plz Help Me.....

    Reply
  • How do you do this in C#

    Posted by Legacy on 12/11/2003 12:00am

    Originally posted by: Ray Johnson


    This is exactly what I need to do. But I need to do it as part of a larger C# app. I've been trying to set up the DLLImport stuff to call the wininet dll from C#. Works for some of the simplier calls in winint - like InternetGetConnectedState.

    Where I'm stumped is how to declare the functions and structures for calls to FindFirstUrlCacheEntry and such. The memory allocation and such makes this not nearly as straight forward.

    Has anyone done this for C#?

    Ray

    • How do u do this in C#

      Posted by saravanan_article on 01/30/2006 11:22pm

      this is exactly what i need to do but these things need to be do in C#. Please post the code.

      Reply
    Reply
  • Great Solution

    Posted by Legacy on 12/02/2003 12:00am

    Originally posted by: milind

    Greate Solution... works perfect ... thanks for code
    ...

    Reply
  • good code

    Posted by Legacy on 09/17/2003 12:00am

    Originally posted by: 박진삼

    정말 좋네요
    임시파일들이 모두 지워졌습니다..
    그리고 아쉬운 점이 있다면 임의의 파일만 지울수 있었으면
    하는 소망이 있네요...^^

    Bye...

    Reply
  • Trying to make a DLL out of this program

    Posted by Legacy on 08/29/2003 12:00am

    Originally posted by: SA

    Hello All,
    
    I am trying to create a DLL from this clear temp files program using Visual Studio .NET and I get a lot of errors like:
    <error>
    error C2061: syntax error : identifier 'HINTERNET'
    error C2059: syntax error : ';'
    error C2061: syntax error : identifier 'DWORD'
    .....
    more than 100 errors.
    </error>

    I have included the following at the start of the file:
    <code>
    #pragma comment(lib, "WinInet.lib")
    </code>

    Here is the command I use to create the dll:
    <code>
    cl -I -LD ClearTempFile.c -Fecookie.dll
    </code>

    Can someone tell me what am I doing wrong?

    Thanks

    Reply
  • how to find ip address of user connected to internet

    Posted by Legacy on 08/19/2003 12:00am

    Originally posted by: adlubadlu

    suggest me how to find ip address of user connected to internet using visual basic 6

    Reply
  • Loading, Please Wait ...

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: December 11, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Market pressures to move more quickly and develop innovative applications are forcing organizations to rethink how they develop and release applications. The combination of public clouds and physical back-end infrastructures are a means to get applications out faster. However, these hybrid solutions complicate DevOps adoption, with application delivery pipelines that span across complex hybrid cloud and non-cloud environments. Check out this …

  • With the average hard drive now averaging one terabyte in size, the fallout from the explosion of user-created data has become an overwhelming volume of potential evidence that law-enforcement and corporate investigators spend countless hours examining. Join Us and SANS' Rob Lee for our 45-minute webinar, A Triage and Collection Strategy for Time-Sensitive Investigations, will demonstrate how to: Identify the folders and files that often contain key insights Reduce the time spent sifting through content by …

Most Popular Programming Stories

More for Developers

RSS Feeds