Vider le cache internet (vb.net 2003)

Contenu du snippet

Adaptation d'un source C# de la MSDN de Microsoft :
Suppression du cache d'internet explorer en VB.NET 2003

Source / Exemple :


Imports System
Imports System.Runtime.InteropServices
Public Class clsIE
    ' 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
    ' File not found.
    Const ERROR_FILE_NOT_FOUND As Integer = &H2
    ' No more items have been found.
    Const ERROR_NO_MORE_ITEMS As Integer = 259

    ' 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 Short
        <FieldOffset(4)> Public lpszSourceUrlName As IntPtr
        <FieldOffset(8)> Public lpszLocalFileName As IntPtr
        <FieldOffset(12)> Public CacheEntryType As Short
        <FieldOffset(16)> Public dwUseCount As Short
        <FieldOffset(20)> Public dwHitRate As Short
        <FieldOffset(24)> Public dwSizeLow As Short
        <FieldOffset(28)> Public dwSizeHigh As Short
        <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 Short
        <FieldOffset(72)> Public lpszFileExtension As IntPtr
        <FieldOffset(76)> Public dwReserved As Short
        <FieldOffset(76)> Public dwExemptDelta As Short
    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)> _
    Shared Function FindFirstUrlCacheGroup( _
        ByVal dwFlags As Integer, _
        ByVal dwFilter As Integer, _
        ByVal lpSearchCondition As IntPtr, _
        ByVal dwSearchCondition As Integer, _
        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", _
        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", _
        SetLastError:=True, _
        CharSet:=CharSet.Auto, _
        EntryPoint:="DeleteUrlCacheGroup", _
        CallingConvention:=CallingConvention.StdCall)> _
    Shared Function DeleteUrlCacheGroup( _
        ByVal GroupId As Long, _
        ByVal dwFlags As Integer, _
        ByVal 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)> _
    Shared Function FindFirstUrlCacheEntry( _
        <MarshalAs(UnmanagedType.LPTStr)> ByVal lpszUrlSearchPattern As String, _
        ByVal 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)> _
    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", _
        SetLastError:=True, _
        CharSet:=CharSet.Auto, _
        EntryPoint:="DeleteUrlCacheEntryA", _
        CallingConvention:=CallingConvention.StdCall)> _
    Shared Function DeleteUrlCacheEntry( _
        ByVal lpszUrlName As IntPtr) As Boolean
    End Function

    <STAThread()> _
    Public Sub sbDeleteALL(Optional ByVal Args() As String = Nothing)
        Try
            ' 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 INTERNET_CACHE_ENTRY_INFOA
            Dim enumHandle As IntPtr = IntPtr.Zero
            Dim returnValue As Boolean = False

            enumHandle = FindFirstUrlCacheGroup(0, CACHEGROUP_SEARCH_ALL, IntPtr.Zero, 0, groupId, IntPtr.Zero)
            If enumHandle.ToInt32 <> IntPtr.Zero.ToInt32 And _
                    ERROR_NO_MORE_ITEMS = Marshal.GetLastWin32Error() Then
                Return
            End If
            While (True)
                returnValue = DeleteUrlCacheGroup(groupId, CACHEGROUP_FLAG_FLUSHURL_ONDELETE, IntPtr.Zero)
                If Not returnValue And ERROR_FILE_NOT_FOUND = Marshal.GetLastWin32Error() Then
                    returnValue = FindNextUrlCacheGroup(enumHandle, groupId, IntPtr.Zero)
                End If

                If Not returnValue And (ERROR_NO_MORE_ITEMS = Marshal.GetLastWin32Error() _
                        Or ERROR_FILE_NOT_FOUND = Marshal.GetLastWin32Error()) Then
                    Exit While
                End If
            End While

            enumHandle = FindFirstUrlCacheEntry(String.Empty, IntPtr.Zero, _
                    cacheEntryInfoBufferSizeInitial)
            If enumHandle.ToInt32 <> IntPtr.Zero.ToInt32 And _
                    ERROR_NO_MORE_ITEMS = Marshal.GetLastWin32Error() Then
                Return
            End If

            cacheEntryInfoBufferSize = cacheEntryInfoBufferSizeInitial
            cacheEntryInfoBuffer = Marshal.AllocHGlobal(cacheEntryInfoBufferSize)
            enumHandle = FindFirstUrlCacheEntry(String.Empty, cacheEntryInfoBuffer, _
                cacheEntryInfoBufferSizeInitial)

            While (True)
                internetCacheEntry = Marshal.PtrToStructure(cacheEntryInfoBuffer, _
                    GetType(INTERNET_CACHE_ENTRY_INFOA))

                cacheEntryInfoBufferSizeInitial = cacheEntryInfoBufferSize
                returnValue = DeleteUrlCacheEntry(internetCacheEntry.lpszSourceUrlName)
                If Not returnValue Then
                    returnValue = FindNextUrlCacheEntry(enumHandle, cacheEntryInfoBuffer, _
                        cacheEntryInfoBufferSizeInitial)
                End If
                If Not returnValue And ERROR_NO_MORE_ITEMS = 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 = FindNextUrlCacheEntry(enumHandle, cacheEntryInfoBuffer, _
                        cacheEntryInfoBufferSizeInitial)
                End If
            End While
            Marshal.FreeHGlobal(cacheEntryInfoBuffer)
        Catch ex As Exception
            gMsg.sbMsgCr(ex.Message, "clsIE.sbDeleteALL")
        End Try
    End Sub
End Class

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.