Chinaunix首页 | 论坛 | 博客
  • 博客访问: 5463743
  • 博文数量: 671
  • 博客积分: 10010
  • 博客等级: 上将
  • 技术积分: 7310
  • 用 户 组: 普通用户
  • 注册时间: 2006-07-14 09:56
文章分类

全部博文(671)

文章存档

2011年(1)

2010年(2)

2009年(24)

2008年(271)

2007年(319)

2006年(54)

我的朋友

分类: C/C++

2008-02-18 16:01:46

 

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 
//
// 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;
}
阅读(918) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~