如何清除应用程序承载 WebBrowser 控件时缓存

注意:这篇文章是由无人工介入的自动的机器翻译系统翻译完成。这些文章是微软为不懂英语的用户提供的, 以使他们能够理解这些文章的内容。微软不保证机器翻译的正确度,也不对由于内容的误译或者客户对它的使用所引起的任何直接的, 或间接的可能的问题负责。
文章编号 : 262110
最后修改 : 2007年3月29日
修订 : 3.1

概要

当应用程序承载 WebBrowser 控件, 可能需要以编程方式清除缓存。 通过 WebBrowser 控件的接口没有此功能。 本文解释如何使用 WinInet API 函数以直接清除缓存。

回到顶端

更多信息

使用 WinInet API FindFirstURLCacheEntry 来找到第一缓存条目和 FindNextUrlCacheEntry 用于列举缓存。 使用 DeleteUrlCacheEntry 来删除每个条目。

注意 FindFirstUrlCacheGroup DeleteUrlCacheGroup FindNextUrlCacheGroup, 以及正在使用下例中。 这些 API 函数以便正确检查作为显示需要防止错误有只成为用 Internet Explorer 5。

在下面步骤介绍如何在 VisualBasic 中使用 WinInet API 来清除缓存中所有文件。

1. 新建一个 VisualBasic 标准 EXE 项目。
2. 创建 form 1 下一个命令按钮。
3. 以下代码粘贴到模块是 form 1:
Option Explicit
            Private Declare Function FindFirstUrlCacheGroup Lib "wininet.dll" ( _
            ByVal dwFlags As Long, _
            ByVal dwFilter As Long, _
            ByRef lpSearchCondition As Long, _
            ByVal dwSearchCondition As Long, _
            ByRef lpGroupId As Date, _
            ByRef lpReserved As Long) As Long
            Private Declare Function FindNextUrlCacheGroup Lib "wininet.dll" ( _
            ByVal hFind As Long, _
            ByRef lpGroupId As Date, _
            ByRef lpReserved As Long) As Long
            Private Declare Function DeleteUrlCacheGroup Lib "wininet.dll" ( _
            ByVal sGroupID As Date, _
            ByVal dwFlags As Long, _
            ByRef lpReserved As Long) As Long
            Private Declare Function FindFirstUrlCacheEntry Lib "wininet.dll" Alias "FindFirstUrlCacheEntryA" ( _
            ByVal lpszUrlSearchPattern As String, _
            ByRef lpFirstCacheEntryInfo As INTERNET_CACHE_ENTRY_INFO, _
            ByRef lpdwFirstCacheEntryInfoBufferSize As Long) As Long
            Private Type INTERNET_CACHE_ENTRY_INFO
            dwStructSize As Long
            szRestOfData(1024) As Long
            End Type
            Private Declare Function DeleteUrlCacheEntry Lib "wininet.dll" Alias "DeleteUrlCacheEntryA" ( _
            ByVal lpszUrlName As Long) As Long
            Private Declare Function FindNextUrlCacheEntry Lib "wininet.dll" Alias "FindNextUrlCacheEntryA" ( _
            ByVal hEnumHandle As Long, _
            ByRef lpNextCacheEntryInfo As INTERNET_CACHE_ENTRY_INFO, _
            ByRef lpdwNextCacheEntryInfoBufferSize As Long) As Long
            Private Const CACHGROUP_SEARCH_ALL = &H0
            Private Const ERROR_NO_MORE_FILES = 18
            Private Const ERROR_NO_MORE_ITEMS = 259
            Private Const CACHEGROUP_FLAG_FLUSHURL_ONDELETE = &H2
            Private Const BUFFERSIZE = 2048
            Private Sub Command1_Click()
            Dim sGroupID As Date
            Dim hGroup As Long
            Dim hFile As Long
            Dim sEntryInfo As INTERNET_CACHE_ENTRY_INFO
            Dim iSize As Long
            On Error Resume Next
            ' Delete the groups
            hGroup = FindFirstUrlCacheGroup(0, 0, 0, 0, sGroupID, 0)
            ' To avoid error using it with IE4 as FindFirstUrlCacheGroup is not implemented
            If Err.Number <> 453 Then
            If (hGroup = 0) And (Err.LastDllError <> 2) Then
            MsgBox "An error occurred enumerating the cache groups" & Err.LastDllError
            Exit Sub
            End If
            Else
            Err.Clear
            End If
            If (hGroup <> 0) Then
            'we succeeded in finding the first cache group.. enumerate and
            'delete
            Do
            If (0 = DeleteUrlCacheGroup(sGroupID, CACHEGROUP_FLAG_FLUSHURL_ONDELETE, 0)) Then
            ' To avoid error using it with IE4 as FindFirstUrlCacheGroup is not implemented
            If Err.Number <> 453 Then
            MsgBox "Error deleting cache group " & Err.LastDllError
            Exit Sub
            Else
            Err.Clear
            End If
            End If
            iSize = BUFFERSIZE
            If (0 = FindNextUrlCacheGroup(hGroup, sGroupID, iSize)) And (Err.LastDllError <> 2) Then
            MsgBox "Error finding next url cache group! - " & Err.LastDllError
            End If
            Loop Until Err.LastDllError = 2
            End If
            ' Delete the files
            sEntryInfo.dwStructSize = 80
            iSize = BUFFERSIZE
            hFile = FindFirstUrlCacheEntry(0, sEntryInfo, iSize)
            If (hFile = 0) Then
            If (Err.LastDllError = ERROR_NO_MORE_ITEMS) Then
            GoTo done
            End If
            MsgBox "ERROR: FindFirstUrlCacheEntry - " & Err.LastDllError
            Exit Sub
            End If
            Do
            If (0 = DeleteUrlCacheEntry(sEntryInfo.szRestOfData(0))) _
            And (Err.LastDllError <> 2) Then
            Err.Clear
            End If
            iSize = BUFFERSIZE
            If (0 = FindNextUrlCacheEntry(hFile, sEntryInfo, iSize)) And (Err.LastDllError <> ERROR_NO_MORE_ITEMS) Then
            MsgBox "Error:  Unable to find the next cache entry - " & Err.LastDllError
            Exit Sub
            End If
            Loop Until Err.LastDllError = ERROR_NO_MORE_ITEMS
            done:
            MsgBox "cache cleared"
            Command1.Enabled = True
            End Sub
            
4. 运行项目并单击 Command 。 计算机中缓存将被清除。
posted @ 2007-07-24 11:38  jetz  阅读(11653)  评论(2编辑  收藏  举报