zoukankan      html  css  js  c++  java
  • 清除浏览器缓存

      1 using System;
    2 using System.Collections.Generic;
    3 using System.Linq;
    4 using System.Text;
    5 using System.Runtime.InteropServices;
    6
    7 namespace LikeIE
    8 {
    9 public class DeleteCache
    10 {
    11 // For PInvoke: Contains information about an entry in the Internet cache
    12 [StructLayout(LayoutKind.Explicit, Size = 80)]
    13 public struct INTERNET_CACHE_ENTRY_INFOA
    14 {
    15 [FieldOffset(0)]
    16 public uint dwStructSize;
    17 [FieldOffset(4)]
    18 public IntPtr lpszSourceUrlName;
    19 [FieldOffset(8)]
    20 public IntPtr lpszLocalFileName;
    21 [FieldOffset(12)]
    22 public uint CacheEntryType;
    23 [FieldOffset(16)]
    24 public uint dwUseCount;
    25 [FieldOffset(20)]
    26 public uint dwHitRate;
    27 [FieldOffset(24)]
    28 public uint dwSizeLow;
    29 [FieldOffset(28)]
    30 public uint dwSizeHigh;
    31 [FieldOffset(32)]
    32 public FILETIME LastModifiedTime;
    33 [FieldOffset(40)]
    34 public FILETIME ExpireTime;
    35 [FieldOffset(48)]
    36 public FILETIME LastAccessTime;
    37 [FieldOffset(56)]
    38 public FILETIME LastSyncTime;
    39 [FieldOffset(64)]
    40 public IntPtr lpHeaderInfo;
    41 [FieldOffset(68)]
    42 public uint dwHeaderInfoSize;
    43 [FieldOffset(72)]
    44 public IntPtr lpszFileExtension;
    45 [FieldOffset(76)]
    46 public uint dwReserved;
    47 [FieldOffset(76)]
    48 public uint dwExemptDelta;
    49 }
    50
    51 // For PInvoke: Initiates the enumeration of the cache groups in the Internet cache
    52 [DllImport(@"wininet",
    53 SetLastError = true,
    54 CharSet = CharSet.Auto,
    55 EntryPoint = "FindFirstUrlCacheGroup",
    56 CallingConvention = CallingConvention.StdCall)]
    57 public static extern IntPtr FindFirstUrlCacheGroup(
    58 int dwFlags,
    59 int dwFilter,
    60 IntPtr lpSearchCondition,
    61 int dwSearchCondition,
    62 ref long lpGroupId,
    63 IntPtr lpReserved);
    64
    65 // For PInvoke: Retrieves the next cache group in a cache group enumeration
    66 [DllImport(@"wininet",
    67 SetLastError = true,
    68 CharSet = CharSet.Auto,
    69 EntryPoint = "FindNextUrlCacheGroup",
    70 CallingConvention = CallingConvention.StdCall)]
    71 public static extern bool FindNextUrlCacheGroup(
    72 IntPtr hFind,
    73 ref long lpGroupId,
    74 IntPtr lpReserved);
    75
    76 // For PInvoke: Releases the specified GROUPID and any associated state in the cache index file
    77 [DllImport(@"wininet",
    78 SetLastError = true,
    79 CharSet = CharSet.Auto,
    80 EntryPoint = "DeleteUrlCacheGroup",
    81 CallingConvention = CallingConvention.StdCall)]
    82 public static extern bool DeleteUrlCacheGroup(
    83 long GroupId,
    84 int dwFlags,
    85 IntPtr lpReserved);
    86
    87 // For PInvoke: Begins the enumeration of the Internet cache
    88 [DllImport(@"wininet",
    89 SetLastError = true,
    90 CharSet = CharSet.Auto,
    91 EntryPoint = "FindFirstUrlCacheEntryA",
    92 CallingConvention = CallingConvention.StdCall)]
    93 public static extern IntPtr FindFirstUrlCacheEntry(
    94 [MarshalAs(UnmanagedType.LPTStr)] string lpszUrlSearchPattern,
    95 IntPtr lpFirstCacheEntryInfo,
    96 ref int lpdwFirstCacheEntryInfoBufferSize);
    97
    98 // For PInvoke: Retrieves the next entry in the Internet cache
    99 [DllImport(@"wininet",
    100 SetLastError = true,
    101 CharSet = CharSet.Auto,
    102 EntryPoint = "FindNextUrlCacheEntryA",
    103 CallingConvention = CallingConvention.StdCall)]
    104 public static extern bool FindNextUrlCacheEntry(
    105 IntPtr hFind,
    106 IntPtr lpNextCacheEntryInfo,
    107 ref int lpdwNextCacheEntryInfoBufferSize);
    108
    109 // For PInvoke: Removes the file that is associated with the source name from the cache, if the file exists
    110 [DllImport(@"wininet",
    111 SetLastError = true,
    112 CharSet = CharSet.Auto,
    113 EntryPoint = "DeleteUrlCacheEntryA",
    114 CallingConvention = CallingConvention.StdCall)]
    115 public static extern bool DeleteUrlCacheEntry(
    116 IntPtr lpszUrlName);
    117
    118 public static void ClearedIECache()
    119 {
    120 // Indicates that all of the cache groups in the user's system should be enumerated
    121 const int CACHEGROUP_SEARCH_ALL = 0x0;
    122 // Indicates that all the cache entries that are associated with the cache group
    123 // should be deleted, unless the entry belongs to another cache group.
    124 const int CACHEGROUP_FLAG_FLUSHURL_ONDELETE = 0x2;
    125 // File not found.
    126 const int ERROR_FILE_NOT_FOUND = 0x2;
    127 // No more items have been found.
    128 const int ERROR_NO_MORE_ITEMS = 259;
    129 // Pointer to a GROUPID variable
    130 long groupId = 0;
    131
    132 // Local variables
    133 int cacheEntryInfoBufferSizeInitial = 0;
    134 int cacheEntryInfoBufferSize = 0;
    135 IntPtr cacheEntryInfoBuffer = IntPtr.Zero;
    136 LikeIE.DeleteCache.INTERNET_CACHE_ENTRY_INFOA internetCacheEntry;
    137 IntPtr enumHandle = IntPtr.Zero;
    138 bool returnValue = false;
    139
    140 // Delete the groups first.
    141 // Groups may not always exist on the system.
    142 // For more information, visit the following Microsoft Web site:
    143 // http://msdn.microsoft.com/library/?url=/workshop/networking/wininet/overview/cache.asp
    144 // By default, a URL does not belong to any group. Therefore, that cache may become
    145 // empty even when the CacheGroup APIs are not used because the existing URL does not belong to any group.
    146 enumHandle = DeleteCache.FindFirstUrlCacheGroup(0, CACHEGROUP_SEARCH_ALL, IntPtr.Zero, 0, ref groupId, IntPtr.Zero);
    147 // If there are no items in the Cache, you are finished.
    148 if (enumHandle != IntPtr.Zero && ERROR_NO_MORE_ITEMS == Marshal.GetLastWin32Error())
    149 return;
    150
    151 // Loop through Cache Group, and then delete entries.
    152 while (true)
    153 {
    154 // Delete a particular Cache Group.
    155 returnValue = DeleteCache.DeleteUrlCacheGroup(groupId, CACHEGROUP_FLAG_FLUSHURL_ONDELETE, IntPtr.Zero);
    156 if (!returnValue && ERROR_FILE_NOT_FOUND == Marshal.GetLastWin32Error())
    157 {
    158 returnValue = DeleteCache.FindNextUrlCacheGroup(enumHandle, ref groupId, IntPtr.Zero);
    159 }
    160
    161 if (!returnValue && (ERROR_NO_MORE_ITEMS == Marshal.GetLastWin32Error() || ERROR_FILE_NOT_FOUND == Marshal.GetLastWin32Error()))
    162 break;
    163 }
    164
    165 // Start to delete URLs that do not belong to any group.
    166 enumHandle = DeleteCache.FindFirstUrlCacheEntry(null, IntPtr.Zero, ref cacheEntryInfoBufferSizeInitial);
    167 if (enumHandle == IntPtr.Zero && ERROR_NO_MORE_ITEMS == Marshal.GetLastWin32Error())
    168 return;
    169
    170 cacheEntryInfoBufferSize = cacheEntryInfoBufferSizeInitial;
    171 cacheEntryInfoBuffer = Marshal.AllocHGlobal(cacheEntryInfoBufferSize);
    172 enumHandle = DeleteCache.FindFirstUrlCacheEntry(null, cacheEntryInfoBuffer, ref cacheEntryInfoBufferSizeInitial);
    173
    174 while (true)
    175 {
    176 internetCacheEntry = (DeleteCache.INTERNET_CACHE_ENTRY_INFOA)Marshal.PtrToStructure(cacheEntryInfoBuffer, typeof(DeleteCache.INTERNET_CACHE_ENTRY_INFOA));
    177
    178 cacheEntryInfoBufferSizeInitial = cacheEntryInfoBufferSize;
    179 returnValue = DeleteCache.DeleteUrlCacheEntry(internetCacheEntry.lpszSourceUrlName);
    180 if (!returnValue)
    181 {
    182 returnValue = DeleteCache.FindNextUrlCacheEntry(enumHandle, cacheEntryInfoBuffer, ref cacheEntryInfoBufferSizeInitial);
    183 }
    184 if (!returnValue && ERROR_NO_MORE_ITEMS == Marshal.GetLastWin32Error())
    185 {
    186 break;
    187 }
    188 if (!returnValue && cacheEntryInfoBufferSizeInitial > cacheEntryInfoBufferSize)
    189 {
    190 cacheEntryInfoBufferSize = cacheEntryInfoBufferSizeInitial;
    191 cacheEntryInfoBuffer = Marshal.ReAllocHGlobal(cacheEntryInfoBuffer, (IntPtr)cacheEntryInfoBufferSize);
    192 returnValue = DeleteCache.FindNextUrlCacheEntry(enumHandle, cacheEntryInfoBuffer, ref cacheEntryInfoBufferSizeInitial);
    193 }
    194 }
    195 Marshal.FreeHGlobal(cacheEntryInfoBuffer);
    196 }
    197 }
    198 }

    调用第二次后,程序会崩溃....

  • 相关阅读:
    CentOS 7系统安装nginx+php
    LINUX VSFTP配置及安装
    linux的mount(挂载)命令详解
    nginx和tomcat的区别
    Linux上安装mysql5.7
    初学Java 二维数组找出最近的两个点
    初学Java 数组统计字母
    Struts2中获取Web元素request、session、application对象的四种方式
    jsp自定义标签
    自定义el函数
  • 原文地址:https://www.cnblogs.com/backkoms/p/2388577.html
Copyright © 2011-2022 走看看