Для тех из нас, кому все еще нужно писать VB.NET, я перенес код, опубликованный Сержем Саганом и Оливером Боком выше, в VB и обнаружил, что он работает хорошо.
Слава богу, что эта ветка существует, я очень долго волновался по этому вопросу.
'
' Modified from code originally found here: http://support.microsoft.com/kb/326201
'
Public Class WebBrowserHelper
Регион "Определения / Импорт DLL"
''' <summary>
''' For PInvoke: Contains information about an entry In the Internet cache
''' </summary>
<StructLayout(LayoutKind.Explicit)>
Public Structure ExemptDeltaOrReserverd
<FieldOffset(0)>
Public dwReserved As UInt32
<FieldOffset(0)>
Public dwExemptDelta As UInt32
End Structure
<StructLayout(LayoutKind.Sequential)>
Public Structure INTERNET_CACHE_ENTRY_INFOA
Public dwStructSize As UInt32
Public lpszSourceUrlName As IntPtr
Public lpszLocalFileName As IntPtr
Public CacheEntryType As UInt32
Public dwUseCount As UInt32
Public dwHitRate As UInt32
Public dwSizeLow As UInt32
Public dwSizeHigh As UInt32
Public LastModifiedTime As FILETIME
Public ExpireTime As FILETIME
Public LastAccessTime As FILETIME
Public LastSyncTime As FILETIME
Public lpHeaderInfo As IntPtr
Public dwHeaderInfoSize As UInt32
Public lpszFileExtension As IntPtr
Public dwExemptDeltaOrReserved As ExemptDeltaOrReserverd
End Structure
' For PInvoke: Initiates the enumeration Of the cache groups In the Internet cache
<DllImport("wininet", SetLastError:=True, CharSet:=CharSet.Auto, EntryPoint:="FindFirstUrlCacheGroup", CallingConvention:=CallingConvention.StdCall)>
Public Shared Function FindFirstUrlCacheGroup(dwFlags As Integer, dwFilter As Integer, lpSearchCondition As IntPtr, dwSearchCondition As Integer, ByRef lpGroupId As Long,
lpReserved As IntPtr) As IntPtr
End Function
' For PInvoke: Retrieves the Next cache group In a cache group enumeration
<DllImport("wininet", SetLastError:=True, CharSet:=CharSet.Auto, EntryPoint:="FindNextUrlCacheGroup", CallingConvention:=CallingConvention.StdCall)>
Public Shared Function FindNextUrlCacheGroup(hFind As IntPtr, ByRef lpGroupId As Long, lpReserved As IntPtr) As Boolean
End Function
' For PInvoke: Releases the specified GROUPID And any associated state In the cache index file
<DllImport("wininet", SetLastError:=True, CharSet:=CharSet.Auto, EntryPoint:="DeleteUrlCacheGroup", CallingConvention:=CallingConvention.StdCall)>
Public Shared Function DeleteUrlCacheGroup(GroupId As Long, dwFlags As Integer, lpReserved As IntPtr) As Boolean
End Function
' For PInvoke: Begins the enumeration Of the Internet cache
<DllImport("wininet", SetLastError:=True, CharSet:=CharSet.Auto, EntryPoint:="FindFirstUrlCacheEntryA", CallingConvention:=CallingConvention.StdCall)>
Public Shared Function FindFirstUrlCacheEntry(<MarshalAs(UnmanagedType.LPTStr)> lpszUrlSearchPattern As String, lpFirstCacheEntryInfo As IntPtr,
ByRef lpdwFirstCacheEntryInfoBufferSize As Integer) As IntPtr
End Function
' For PInvoke: Retrieves the Next entry In the Internet cache
<DllImport("wininet", SetLastError:=True, CharSet:=CharSet.Auto, EntryPoint:="FindNextUrlCacheEntryA", CallingConvention:=CallingConvention.StdCall)>
Public Shared Function FindNextUrlCacheEntry(hFind As IntPtr, lpNextCacheEntryInfo As IntPtr, ByRef lpdwNextCacheEntryInfoBufferSize As Integer) As Boolean
End Function
' For PInvoke: Removes the file that Is associated With the source name from the cache, If the file exists
<DllImport("wininet", SetLastError:=True, CharSet:=CharSet.Auto, EntryPoint:="DeleteUrlCacheEntryA", CallingConvention:=CallingConvention.StdCall)>
Public Shared Function DeleteUrlCacheEntry(lpszUrlName As IntPtr) As Boolean
End Function
Конечный регион
''' <summary>
''' Clears the cache of the web browser
''' </summary>
Public Shared Sub ClearCache()
' Indicates that all of the cache groups in the user's system should be enumerated
Const CACHEGROUP_SEARCH_ALL As Integer = &H0
' Indicates that all the cache entries that are associated with the cache group
' should be deleted, unless the entry belongs to another cache group.
Const CACHEGROUP_FLAG_FLUSHURL_ONDELETE As Integer = &H2
Const ERROR_INSUFFICIENT_BUFFER As Integer = &H7A
' Delete the groups first.
' Groups may Not always exist on the system.
' For more information, visit the following Microsoft Web site:
' http//msdn.microsoft.com/library/?url=/workshop/networking/wininet/overview/cache.asp
' By default, a URL does Not belong to any group. Therefore, that cache may become
' empty even when the CacheGroup APIs are Not used because the existing URL does Not belong to any group.
Dim groupId As Long = 0
Dim enumHandle As IntPtr = FindFirstUrlCacheGroup(0, CACHEGROUP_SEARCH_ALL, IntPtr.Zero, 0, groupId, IntPtr.Zero)
If (enumHandle <> IntPtr.Zero) Then
Dim more As Boolean
Do
' Delete a particular Cache Group.
DeleteUrlCacheGroup(groupId, CACHEGROUP_FLAG_FLUSHURL_ONDELETE, IntPtr.Zero)
more = FindNextUrlCacheGroup(enumHandle, groupId, IntPtr.Zero)
Loop While (more)
End If
' Start to delete URLs that do Not belong to any group.
Dim cacheEntryInfoBufferSizeInitial As Integer = 0
FindFirstUrlCacheEntry(Nothing, IntPtr.Zero, cacheEntryInfoBufferSizeInitial) ' should always fail because buffer Is too small
If Marshal.GetLastWin32Error() = ERROR_INSUFFICIENT_BUFFER Then
Dim cacheEntryInfoBufferSize As Integer = cacheEntryInfoBufferSizeInitial
Dim cacheEntryInfoBuffer As IntPtr = Marshal.AllocHGlobal(cacheEntryInfoBufferSize)
enumHandle = FindFirstUrlCacheEntry(Nothing, cacheEntryInfoBuffer, cacheEntryInfoBufferSizeInitial)
If (enumHandle <> IntPtr.Zero) Then
Dim more As Boolean
Do
Dim internetCacheEntry As INTERNET_CACHE_ENTRY_INFOA = CType(Marshal.PtrToStructure(cacheEntryInfoBuffer, GetType(INTERNET_CACHE_ENTRY_INFOA)), INTERNET_CACHE_ENTRY_INFOA)
cacheEntryInfoBufferSizeInitial = cacheEntryInfoBufferSize
DeleteUrlCacheEntry(internetCacheEntry.lpszSourceUrlName)
more = FindNextUrlCacheEntry(enumHandle, cacheEntryInfoBuffer, cacheEntryInfoBufferSizeInitial)
If Not more AndAlso Marshal.GetLastWin32Error() = ERROR_INSUFFICIENT_BUFFER Then
cacheEntryInfoBufferSize = cacheEntryInfoBufferSizeInitial
cacheEntryInfoBuffer = Marshal.ReAllocHGlobal(cacheEntryInfoBuffer, CType(cacheEntryInfoBufferSize, IntPtr))
more = FindNextUrlCacheEntry(enumHandle, cacheEntryInfoBuffer, cacheEntryInfoBufferSizeInitial)
End If
Loop While (more)
End If
Marshal.FreeHGlobal(cacheEntryInfoBuffer)
End If
End Sub
End Class