You must Sign In to post a response.
  • Category: ASP.NET

    How to delete my temporary internet files

    Can any one plz suggest me how i can delete my temporary internet files
    within my page. I am using C# as the code behind language.
  • #64083
    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: 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)> _
    <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
    '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.
    'File not found.
    '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:
    '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("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

    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:a. On the Tools menu, click Internet Options.
    b. In the Temporary Internet files area on the General tab, click Settings.
    c. Click View Files. Notice that all of the files in the Internet Explorer cache have been deleted.

  • This thread is locked for new responses. Please post your comments and questions as a separate thread.
    If required, refer to the URL of this page in your new post.