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

Article translations Article translations
Article ID: 311289 - View products that this article applies to.
This article was previously published under Q311289
Expand all | Collapse all

On This Page

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.

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.

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:
  1. Start Microsoft Visual Studio .NET.
  2. On the File menu, point to New, and then click Project.
  3. In the New Project dialog box, click Visual Basic Projects under Project Types, and then click Console Application under Templates.
  4. On the Project menu, click Add Class.
  5. 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
            <StructLayout(LayoutKind.Explicit, Size:=80)> _
            Public Structure INTERNET_CACHE_ENTRY_INFOA
            <FieldOffset(0)> Public dwStructSize As UInt32
            <FieldOffset(4)> 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("wininet.dll", SetLastError:=True, _
           CharSet:=CharSet.Auto, _
           EntryPoint:="FindFirstUrlCacheGroup", _
           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("wininet.dll", _
           SetLastError:=True, _
           CharSet:=CharSet.Auto, _
           EntryPoint:="FindNextUrlCacheGroup", _
           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("wininet.dll", _
           SetLastError:=True, _
           CharSet:=CharSet.Auto, _
           EntryPoint:="DeleteUrlCacheGroup", _
           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("wininet.dll", _
           SetLastError:=True, _
           CharSet:=CharSet.Auto, _
           EntryPoint:="FindFirstUrlCacheEntryA", _
           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("wininet.dll", _
           SetLastError:=True, _
           CharSet:=CharSet.Auto, _
           EntryPoint:="FindNextUrlCacheEntryA", _
           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("wininet.dll", _
          SetLastError:=True, _
          CharSet:=CharSet.Auto, _
          EntryPoint:="DeleteUrlCacheEntryA", _
          CallingConvention:=CallingConvention.StdCall)> _
        Shared Function DeleteUrlCacheEntry( _
            ByVal lpszUrlName As IntPtr) As Boolean
        End Function
    
    End Class
    					
  6. 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 (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("Error Deleting: {0}", 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
    					
  7. Compile and then run the project.
  8. To confirm that the temporary Internet files in your cache have been deleted, follow these steps in Microsoft Internet Explorer:
    1. On the Tools menu, click Internet Options.
    2. In the Temporary Internet files area on the General tab, click Settings.
    3. Click View Files. Notice that all of the files in the Internet Explorer cache have been deleted.

REFERENCES

The code sample in this article uses the Interop namespace that is part of the Visual Studio .NET runtime. For more information, visit the following Microsoft Developer Network (MSDN) Web site:
http://msdn2.microsoft.com/en-us/library/sd10k43k(vs.71).aspx
For information about Microsoft .NET development, visit the following MSDN Web site:
http://msdn.microsoft.com/en-us/library/ms950381.aspx
For more information about the syntax of the WinInet caching functions, visit the following MSDN Web site:
http://msdn.microsoft.com/en-us/library/aa383928.aspx
For additional information, click the following article number to view the article in the Microsoft Knowledge Base:
262110 How to clear cache when your application hosts a WebBrowser control
For more information about developing Web-based solutions for Microsoft Internet Explorer, visit the following Microsoft Web site:

http://msdn.microsoft.com/ie/

Properties

Article ID: 311289 - Last Review: March 24, 2009 - Revision: 5.0
APPLIES TO
  • Microsoft Visual Basic .NET 2002 Standard Edition
  • Microsoft Visual Basic .NET 2003 Standard Edition
Keywords: 
kbcaching kbcominterop kbgrpdsinet kbhowtomaster kbwebbrowser KB311289

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com