Microsoft KB Archive/311289

= How to clear the cache when your application hosts a WebBrowser control in Visual Basic .NET =

Article ID: 311289

Article Last Modified on 5/21/2007

-

APPLIES TO


 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft Visual Basic .NET 2003 Standard Edition

-



This article was previously published under Q311289



For a Microsoft Visual C# .NET version of this article, see 326201.

This article refers to the following Microsoft .NET Framework Class Library namespace:
 * System.Runtime.InteropServices

IN THIS TASK
SUMMARY
 * WinInet Functions
 * Steps to Clear the Cache in Visual Basic .NET

REFERENCES



SUMMARY
This step-by-step article describes how to use WinInet application programming interface (API) functions to clear the cache directly.

You may have to clear the cache programmatically when your application hosts a WebBrowser control. However, this feature is not available through the interfaces of the WebBrowser control.

back to the top

WinInet Functions
To clear the cache directly, you can use the following WinInet functions:
 * Use the FindFirstURLCacheEntry function to find the first cache entry.
 * Use the FindNextUrlCacheEntry function to enumerate through the cache.
 * Use the DeleteUrlCacheEntry function to delete each entry.

The code sample in this article uses all of these functions.

NOTE: These functions are only available with Microsoft Internet Explorer 5. Therefore, you must include the appropriate checks (which the code sample in this article includes) to prevent errors.

back to the top

Steps to Clear the Cache in Visual Basic .NET
To use the WinInet functions in Visual Basic .NET to clear all of the files in the cache, follow these steps:  Start Microsoft Visual Studio .NET. On the File menu, point to New, and then click Project. In the New Project dialog box, click Visual Basic Projects under Project Types, and then click Console Application under Templates. On the Project menu, click Add Class.  Add the following code to the Class1 class: Imports System.Runtime.InteropServices 'Class for deleting the cache. Public Class Class1 'For PInvoke: Contains information about an entry in the Internet cache  _ Public Structure INTERNET_CACHE_ENTRY_INFOA  Public dwStructSize As UInt32  Public lpszSourceUrlName As IntPtr <FieldOffset(8)> Public lpszLocalFileName As IntPtr <FieldOffset(12)> Public CacheEntryType As UInt32 <FieldOffset(16)> Public dwUseCount As UInt32 <FieldOffset(20)> Public dwHitRate As UInt32 <FieldOffset(24)> Public dwSizeLow As UInt32 <FieldOffset(28)> Public dwSizeHigh As UInt32 <FieldOffset(32)> Public LastModifiedTime As FILETIME <FieldOffset(40)> Public ExpireTime As FILETIME <FieldOffset(48)> Public LastAccessTime As FILETIME <FieldOffset(56)> Public LastSyncTime As FILETIME <FieldOffset(64)> Public lpHeaderInfo As IntPtr <FieldOffset(68)> Public dwHeaderInfoSize As UInt32 <FieldOffset(72)> Public lpszFileExtension As IntPtr <FieldOffset(76)> Public dwReserved As UInt32 <FieldOffset(76)> Public dwExemptDelta As UInt32 End Structure

'For PInvoke: Initiates the enumeration of the cache groups in the Internet cache <DllImport(&quot;wininet.dll&quot;, SetLastError:=True, _      CharSet:=CharSet.Auto, _       EntryPoint:=&quot;FindFirstUrlCacheGroup&quot;, _       CallingConvention:=CallingConvention.StdCall)> _ Shared Function FindFirstUrlCacheGroup( _       ByVal dwFlags As Int32, _        ByVal dwFilter As Integer, _        ByVal lpSearchCondition As IntPtr, _        ByVal dwSearchCondition As Int32, _        ByRef lpGroupId As Long, _        ByVal lpReserved As IntPtr) As IntPtr End Function

'For PInvoke: Retrieves the next cache group in a cache group enumeration <DllImport(&quot;wininet.dll&quot;, _      SetLastError:=True, _       CharSet:=CharSet.Auto, _       EntryPoint:=&quot;FindNextUrlCacheGroup&quot;, _       CallingConvention:=CallingConvention.StdCall)> _ Shared Function FindNextUrlCacheGroup( _       ByVal hFind As IntPtr, _        ByRef lpGroupId As Long, _        ByVal lpReserved As IntPtr) As Boolean End Function

'For PInvoke: Releases the specified GROUPID and any associated state in the cache index file <DllImport(&quot;wininet.dll&quot;, _      SetLastError:=True, _       CharSet:=CharSet.Auto, _       EntryPoint:=&quot;DeleteUrlCacheGroup&quot;, _       CallingConvention:=CallingConvention.StdCall)> _ Shared Function DeleteUrlCacheGroup( _       ByVal GroupId As Long, _        ByVal dwFlags As Int32, _        ByVal lpReserved As IntPtr) As Boolean End Function

'For PInvoke: Begins the enumeration of the Internet cache <DllImport(&quot;wininet.dll&quot;, _      SetLastError:=True, _       CharSet:=CharSet.Auto, _       EntryPoint:=&quot;FindFirstUrlCacheEntryA&quot;, _       CallingConvention:=CallingConvention.StdCall)> _ Shared Function FindFirstUrlCacheEntry( _   <MarshalAs(UnmanagedType.LPStr)> ByVal lpszUrlSearchPattern As String, _         ByVal lpFirstCacheEntryInfo As IntPtr, _         ByRef lpdwFirstCacheEntryInfoBufferSize As Int32) As IntPtr End Function

'For PInvoke: Retrieves the next entry in the Internet cache <DllImport(&quot;wininet.dll&quot;, _      SetLastError:=True, _       CharSet:=CharSet.Auto, _       EntryPoint:=&quot;FindNextUrlCacheEntryA&quot;, _       CallingConvention:=CallingConvention.StdCall)> _ Shared Function FindNextUrlCacheEntry( _         ByVal hFind As IntPtr, _          ByVal 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(&quot;wininet.dll&quot;, _     SetLastError:=True, _      CharSet:=CharSet.Auto, _      EntryPoint:=&quot;DeleteUrlCacheEntryA&quot;, _      CallingConvention:=CallingConvention.StdCall)> _ Shared Function DeleteUrlCacheEntry( _       ByVal lpszUrlName As IntPtr) As Boolean End Function

End Class </li>  Add the following code to the Module1.vb module: Imports System.Runtime.InteropServices Module Module1

Sub Main 'Indicates that all of the cache groups in the user's system should be enumerated Const CACHEGROUP_SEARCH_ALL = &H0 'Indicates that all of 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 = &H2 'File not found. Const ERROR_FILE_NOT_FOUND = &H2 'No more items have been found. Const ERROR_NO_MORE_ITEMS = 259 'Pointer to a GROUPID variable Dim groupId As Long = 0

'Local variables Dim cacheEntryInfoBufferSizeInitial As Integer = 0 Dim cacheEntryInfoBufferSize As Integer = 0 Dim cacheEntryInfoBuffer As IntPtr = IntPtr.Zero Dim internetCacheEntry As Class1.INTERNET_CACHE_ENTRY_INFOA Dim enumHandle As IntPtr = IntPtr.Zero Dim returnValue As Boolean = False

'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 CacheGroup APIs are not used because the existing URL does not belong to any group.

enumHandle = Class1.FindFirstUrlCacheGroup(0, CACHEGROUP_SEARCH_ALL, IntPtr.Zero, 0, groupId, IntPtr.Zero) 'If there are no items in the Cache, you are finished. If (Not enumHandle.Equals(IntPtr.Zero) And ERROR_NO_MORE_ITEMS.Equals(Marshal.GetLastWin32Error)) Then Exit Sub End If

'Loop through Cache Group, and then delete entries. While (True) 'Delete a particular Cache Group. returnValue = Class1.DeleteUrlCacheGroup(groupId, CACHEGROUP_FLAG_FLUSHURL_ONDELETE, IntPtr.Zero)

If (Not returnValue And ERROR_FILE_NOT_FOUND.Equals(Marshal.GetLastWin32Error)) Then returnValue = Class1.FindNextUrlCacheGroup(enumHandle, groupId, IntPtr.Zero) End If

If (Not returnValue And (ERROR_NO_MORE_ITEMS.Equals(Marshal.GetLastWin32Error) Or ERROR_FILE_NOT_FOUND.Equals(Marshal.GetLastWin32Error))) Then Exit While End If       End While 'Start to delete URLs that do not belong to any group. enumHandle = Class1.FindFirstUrlCacheEntry(vbNull, IntPtr.Zero, cacheEntryInfoBufferSizeInitial)

If (Not enumHandle.Equals(IntPtr.Zero) And ERROR_NO_MORE_ITEMS.Equals(Marshal.GetLastWin32Error)) Then Exit Sub End If

cacheEntryInfoBufferSize = cacheEntryInfoBufferSizeInitial cacheEntryInfoBuffer = Marshal.AllocHGlobal(cacheEntryInfoBufferSize) enumHandle = Class1.FindFirstUrlCacheEntry(vbNull, cacheEntryInfoBuffer, cacheEntryInfoBufferSizeInitial)

While (True) internetCacheEntry = CType(Marshal.PtrToStructure(cacheEntryInfoBuffer, GetType(Class1.INTERNET_CACHE_ENTRY_INFOA)), Class1.INTERNET_CACHE_ENTRY_INFOA) cacheEntryInfoBufferSizeInitial = cacheEntryInfoBufferSize returnValue = Class1.DeleteUrlCacheEntry(internetCacheEntry.lpszSourceUrlName)

If (Not returnValue) Then 'Console.WriteLine(&quot;Error Deleting: {0}&quot;, Marshal.GetLastWin32Error) End If

returnValue = Class1.FindNextUrlCacheEntry(enumHandle, cacheEntryInfoBuffer, cacheEntryInfoBufferSizeInitial) If (Not returnValue And ERROR_NO_MORE_ITEMS.Equals(Marshal.GetLastWin32Error)) Then Exit While End If

If (Not returnValue And cacheEntryInfoBufferSizeInitial > cacheEntryInfoBufferSize) Then

cacheEntryInfoBufferSize = cacheEntryInfoBufferSizeInitial Dim tempIntPtr As New IntPtr(cacheEntryInfoBufferSize) cacheEntryInfoBuffer = Marshal.ReAllocHGlobal(cacheEntryInfoBuffer, tempIntPtr) returnValue = Class1.FindNextUrlCacheEntry(enumHandle, cacheEntryInfoBuffer, cacheEntryInfoBufferSizeInitial) End If       End While Marshal.FreeHGlobal(cacheEntryInfoBuffer)

End Sub

End Module </li> Compile and then run the project.</li> To confirm that the temporary Internet files in your cache have been deleted, follow these steps in Microsoft Internet Explorer: <ol style="list-style-type: lower-alpha;"> On the Tools menu, click Internet Options.</li> In the Temporary Internet files area on the General tab, click Settings.</li> Click View Files. Notice that all of the files in the Internet Explorer cache have been deleted.</li></ol> </li></ol>

back to the top

<div class="references_section">