Advertisement
sanych_dv

Untitled

Mar 14th, 2018
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.67 KB | None | 0 0
  1. #region Usings
  2. using System;
  3. using System.Runtime.InteropServices;
  4.  
  5. #endregion
  6.  
  7. namespace Utilities.Web.WebBrowserHelper
  8. {
  9.     /// <summary>
  10.     /// Class for clearing the cache
  11.     /// </summary>
  12.     public static class WebBrowserHelper
  13.     {
  14.         #region Definitions/DLL Imports
  15.         /// <summary>
  16.         /// For PInvoke Contains information about an entry in the Internet cache
  17.         /// </summary>
  18.         [StructLayout(LayoutKind.Explicit, Size = 80)]
  19.         public struct INTERNET_CACHE_ENTRY_INFOA
  20.         {
  21.             [FieldOffset(0)]
  22.             public uint dwStructSize;
  23.             [FieldOffset(4)]
  24.             public IntPtr lpszSourceUrlName;
  25.             [FieldOffset(8)]
  26.             public IntPtr lpszLocalFileName;
  27.             [FieldOffset(12)]
  28.             public uint CacheEntryType;
  29.             [FieldOffset(16)]
  30.             public uint dwUseCount;
  31.             [FieldOffset(20)]
  32.             public uint dwHitRate;
  33.             [FieldOffset(24)]
  34.             public uint dwSizeLow;
  35.             [FieldOffset(28)]
  36.             public uint dwSizeHigh;
  37.             [FieldOffset(32)]
  38.             public System.Runtime.InteropServices.ComTypes.FILETIME LastModifiedTime;
  39.             [FieldOffset(40)]
  40.             public System.Runtime.InteropServices.ComTypes.FILETIME ExpireTime;
  41.             [FieldOffset(48)]
  42.             public System.Runtime.InteropServices.ComTypes.FILETIME LastAccessTime;
  43.             [FieldOffset(56)]
  44.             public System.Runtime.InteropServices.ComTypes.FILETIME LastSyncTime;
  45.             [FieldOffset(64)]
  46.             public IntPtr lpHeaderInfo;
  47.             [FieldOffset(68)]
  48.             public uint dwHeaderInfoSize;
  49.             [FieldOffset(72)]
  50.             public IntPtr lpszFileExtension;
  51.             [FieldOffset(76)]
  52.             public uint dwReserved;
  53.             [FieldOffset(76)]
  54.             public uint dwExemptDelta;
  55.         }
  56.  
  57.         // For PInvoke Initiates the enumeration of the cache groups in the Internet cache
  58.         [DllImport(@"wininet",
  59.           SetLastError = true,
  60.           CharSet = CharSet.Auto,
  61.           EntryPoint = "FindFirstUrlCacheGroup",
  62.           CallingConvention = CallingConvention.StdCall)]
  63.         public static extern IntPtr FindFirstUrlCacheGroup(
  64.           int dwFlags,
  65.           int dwFilter,
  66.           IntPtr lpSearchCondition,
  67.           int dwSearchCondition,
  68.           ref long lpGroupId,
  69.           IntPtr lpReserved);
  70.  
  71.         // For PInvoke Retrieves the next cache group in a cache group enumeration
  72.         [DllImport(@"wininet",
  73.           SetLastError = true,
  74.           CharSet = CharSet.Auto,
  75.           EntryPoint = "FindNextUrlCacheGroup",
  76.           CallingConvention = CallingConvention.StdCall)]
  77.         public static extern bool FindNextUrlCacheGroup(
  78.           IntPtr hFind,
  79.           ref long lpGroupId,
  80.           IntPtr lpReserved);
  81.  
  82.         // For PInvoke Releases the specified GROUPID and any associated state in the cache index file
  83.         [DllImport(@"wininet",
  84.           SetLastError = true,
  85.           CharSet = CharSet.Auto,
  86.           EntryPoint = "DeleteUrlCacheGroup",
  87.           CallingConvention = CallingConvention.StdCall)]
  88.         public static extern bool DeleteUrlCacheGroup(
  89.           long GroupId,
  90.           int dwFlags,
  91.           IntPtr lpReserved);
  92.  
  93.         // For PInvoke Begins the enumeration of the Internet cache
  94.         [DllImport(@"wininet",
  95.           SetLastError = true,
  96.           CharSet = CharSet.Auto,
  97.          EntryPoint = "FindFirstUrlCacheEntryA",
  98.          CallingConvention = CallingConvention.StdCall)]
  99.         public static extern IntPtr FindFirstUrlCacheEntry(
  100.           [MarshalAs(UnmanagedType.LPTStr)] string lpszUrlSearchPattern,
  101.           IntPtr lpFirstCacheEntryInfo,
  102.           ref int lpdwFirstCacheEntryInfoBufferSize);
  103.  
  104.         // For PInvoke Retrieves the next entry in the Internet cache
  105.         [DllImport(@"wininet",
  106.           SetLastError = true,
  107.           CharSet = CharSet.Auto,
  108.           EntryPoint = "FindNextUrlCacheEntryA",
  109.           CallingConvention = CallingConvention.StdCall)]
  110.         public static extern bool FindNextUrlCacheEntry(
  111.           IntPtr hFind,
  112.           IntPtr lpNextCacheEntryInfo,
  113.           ref int lpdwNextCacheEntryInfoBufferSize);
  114.  
  115.         // For PInvoke Removes the file that is associated with the source name from the cache, if the file exists
  116.         [DllImport(@"wininet",
  117.           SetLastError = true,
  118.           CharSet = CharSet.Auto,
  119.           EntryPoint = "DeleteUrlCacheEntryA",
  120.           CallingConvention = CallingConvention.StdCall)]
  121.         public static extern bool DeleteUrlCacheEntry(
  122.           IntPtr lpszUrlName);
  123.         #endregion
  124.  
  125.         #region Public Static Functions
  126.  
  127.         public static void Main()
  128.         {
  129.             ClearCache();
  130.         }
  131.  
  132.         /// <summary>
  133.         /// Clears the cache of the web browser
  134.         /// </summary>
  135.         public static void ClearCache()
  136.         {
  137.  
  138.             try
  139.             {
  140.  
  141.                 // No more items have been found.
  142.                 const int ERROR_NO_MORE_ITEMS = 259;
  143.  
  144.                 // Local variables
  145.                 int cacheEntryInfoBufferSizeInitial = 0;
  146.                 int cacheEntryInfoBufferSize = 0;
  147.                 IntPtr cacheEntryInfoBuffer = IntPtr.Zero;
  148.                 INTERNET_CACHE_ENTRY_INFOA internetCacheEntry;
  149.                 IntPtr enumHandle = IntPtr.Zero;
  150.                 bool returnValue = false;
  151.  
  152.                 // Start to delete URLs that do not belong to any group.
  153.                 enumHandle = FindFirstUrlCacheEntry(null, IntPtr.Zero, ref cacheEntryInfoBufferSizeInitial);
  154.                 if (enumHandle == IntPtr.Zero && ERROR_NO_MORE_ITEMS == Marshal.GetLastWin32Error())
  155.                     return;
  156.  
  157.                 cacheEntryInfoBufferSize = cacheEntryInfoBufferSizeInitial;
  158.                 cacheEntryInfoBuffer = Marshal.AllocHGlobal(cacheEntryInfoBufferSize);
  159.                 enumHandle = FindFirstUrlCacheEntry(null, cacheEntryInfoBuffer, ref cacheEntryInfoBufferSizeInitial);
  160.  
  161.                 while (true)
  162.                 {
  163.                     internetCacheEntry = (INTERNET_CACHE_ENTRY_INFOA)Marshal.PtrToStructure(cacheEntryInfoBuffer, typeof(INTERNET_CACHE_ENTRY_INFOA));
  164.  
  165.                     string sourceUrlName = Marshal.PtrToStringAnsi(internetCacheEntry.lpszSourceUrlName);
  166.                     cacheEntryInfoBufferSizeInitial = cacheEntryInfoBufferSize;
  167.  
  168.                     if ((sourceUrlName.Contains("instagram.com") || sourceUrlName.Contains("facebook.com")) && sourceUrlName.ToLower().Contains("cookie"))
  169.                     {
  170.                         DeleteUrlCacheEntry(internetCacheEntry.lpszSourceUrlName);
  171.                     }
  172.  
  173.                     returnValue = FindNextUrlCacheEntry(enumHandle, cacheEntryInfoBuffer, ref cacheEntryInfoBufferSizeInitial);
  174.  
  175.                     if (!returnValue && ERROR_NO_MORE_ITEMS == Marshal.GetLastWin32Error())
  176.                     {
  177.                         break;
  178.                     }
  179.                     if (!returnValue && cacheEntryInfoBufferSizeInitial > cacheEntryInfoBufferSize)
  180.                     {
  181.                         cacheEntryInfoBufferSize = cacheEntryInfoBufferSizeInitial;
  182.                         cacheEntryInfoBuffer = Marshal.ReAllocHGlobal(cacheEntryInfoBuffer, (IntPtr)cacheEntryInfoBufferSize);
  183.                         returnValue = FindNextUrlCacheEntry(enumHandle, cacheEntryInfoBuffer, ref cacheEntryInfoBufferSizeInitial);
  184.                     }
  185.                 }
  186.                 Marshal.FreeHGlobal(cacheEntryInfoBuffer);
  187.  
  188.  
  189.             }
  190.             catch
  191.             {
  192.  
  193.                 // error
  194.             }
  195.  
  196.         }
  197.         #endregion
  198.     }
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement