Deleting IE Cache and Cookies using C# Code in WPF

前端 未结 2 885
半阙折子戏
半阙折子戏 2021-01-13 05:18

I am using a WebBrowser control in my WPF application and I am looking to clear the IE cookie cache from code.

I have attempted to use the following code

<         


        
2条回答
  •  醉酒成梦
    2021-01-13 05:59

    Using below function one can delete cookie for the certain host name. If whole cache needs to be cleared then condition "if(sourceUrlName.Contains(hostEntry) && sourceUrlName.ToLower().Contains("cookie"))" should be removed.

        /// 
        /// Internets the set cookie.
        /// 
        /// Name of the LPSZ URL.
        /// Name of the LPSZ cookie.
        /// The LPSZ cookie data.
        /// true if XXXX, false otherwise
        [DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
        static extern bool InternetSetCookie(string lpszUrlName, string lpszCookieName, string lpszCookieData);
    
        /// 
        /// Internets the get cookie.
        /// 
        /// The LPSZ URL.
        /// Name of the LPSZ cookie.
        /// The LPSZ cookie data.
        /// Size of the LPDW.
        /// true if XXXX, false otherwise
        [DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
        static extern bool InternetGetCookie(string lpszUrl, string lpszCookieName, StringBuilder lpszCookieData, ref int lpdwSize);
    
        /// 
        /// Finds the first URL cache entry.
        /// 
        /// The LPSZ URL search pattern.
        /// The lp first cache entry info.
        /// Size of the LPDW first cache entry info buffer.
        /// IntPtr.
        [DllImport(@"wininet",
            SetLastError = true,
            CharSet = CharSet.Auto,
            EntryPoint = "FindFirstUrlCacheEntryA",
            CallingConvention = CallingConvention.StdCall)]
        public static extern IntPtr FindFirstUrlCacheEntry(
            [MarshalAs(UnmanagedType.LPTStr)] string lpszUrlSearchPattern,
            IntPtr lpFirstCacheEntryInfo,
            ref int lpdwFirstCacheEntryInfoBufferSize);
    
        /// 
        /// Finds the next URL cache entry.
        /// 
        /// The h find.
        /// The lp next cache entry info.
        /// Size of the LPDW next cache entry info buffer.
        /// true if XXXX, false otherwise
        [DllImport(@"wininet",
            SetLastError = true,
            CharSet = CharSet.Auto,
            EntryPoint = "FindNextUrlCacheEntryA",
            CallingConvention = CallingConvention.StdCall)]
        public static extern bool FindNextUrlCacheEntry(
            IntPtr hFind,
            IntPtr lpNextCacheEntryInfo,
            ref int lpdwNextCacheEntryInfoBufferSize);
    
        /// 
        /// Deletes the URL cache entry.
        /// 
        /// Name of the LPSZ URL.
        /// true if XXXX, false otherwise
        [DllImport(@"wininet",
            SetLastError = true,
            CharSet = CharSet.Auto,
            EntryPoint = "DeleteUrlCacheEntryA",
            CallingConvention = CallingConvention.StdCall)]
        public static extern bool DeleteUrlCacheEntry(
            IntPtr lpszUrlName);
    
    
        /// 
        /// Clears the IE cache.
        /// 
        /// The URL.
        public static void ClearIECache(string url)
        {
            try
            {
                // No more items have been found.
                const int ERROR_NO_MORE_ITEMS = 259;
    
                string hostEntry = new Uri(url).Host;
    
                // Local variables
                int cacheEntryInfoBufferSizeInitial = 0;
                int cacheEntryInfoBufferSize = 0;
                IntPtr cacheEntryInfoBuffer = IntPtr.Zero;
                INTERNET_CACHE_ENTRY_INFOA internetCacheEntry;
                IntPtr enumHandle = IntPtr.Zero;
                bool returnValue = false;
    
                // Start to delete URLs that do not belong to any group.
                enumHandle = FindFirstUrlCacheEntry(null, IntPtr.Zero, ref cacheEntryInfoBufferSizeInitial);
                if (enumHandle == IntPtr.Zero && ERROR_NO_MORE_ITEMS == Marshal.GetLastWin32Error())
                    return;
    
                cacheEntryInfoBufferSize = cacheEntryInfoBufferSizeInitial;
                cacheEntryInfoBuffer = Marshal.AllocHGlobal(cacheEntryInfoBufferSize);
                enumHandle = FindFirstUrlCacheEntry(null, cacheEntryInfoBuffer, ref cacheEntryInfoBufferSizeInitial);
    
                while (true)
                {
                    internetCacheEntry = (INTERNET_CACHE_ENTRY_INFOA)Marshal.PtrToStructure(cacheEntryInfoBuffer, typeof(INTERNET_CACHE_ENTRY_INFOA));
    
                    string sourceUrlName = Marshal.PtrToStringAnsi(internetCacheEntry.lpszSourceUrlName);
                    cacheEntryInfoBufferSizeInitial = cacheEntryInfoBufferSize;
    
                    if (sourceUrlName.Contains(hostEntry) && sourceUrlName.ToLower().Contains("cookie"))
                    {
                        DeleteUrlCacheEntry(internetCacheEntry.lpszSourceUrlName);
                    }
    
                    returnValue = FindNextUrlCacheEntry(enumHandle, cacheEntryInfoBuffer, ref cacheEntryInfoBufferSizeInitial);
    
                    if (!returnValue && ERROR_NO_MORE_ITEMS == Marshal.GetLastWin32Error())
                    {
                        break;
                    }
                    if (!returnValue && cacheEntryInfoBufferSizeInitial > cacheEntryInfoBufferSize)
                    {
                        cacheEntryInfoBufferSize = cacheEntryInfoBufferSizeInitial;
                        cacheEntryInfoBuffer = Marshal.ReAllocHGlobal(cacheEntryInfoBuffer, (IntPtr)cacheEntryInfoBufferSize);
                        returnValue = FindNextUrlCacheEntry(enumHandle, cacheEntryInfoBuffer, ref cacheEntryInfoBufferSizeInitial);
                    }
                }
    
                Marshal.FreeHGlobal(cacheEntryInfoBuffer);
            }
            catch
            {
                //error
            }
        }
    
    
    
        /// 
    /// Struct INTERNET_CACHE_ENTRY_INFOA
    /// 
    [StructLayout(LayoutKind.Explicit, Size = 80)]
    public struct INTERNET_CACHE_ENTRY_INFOA
    {
        [FieldOffset(0)]
        public uint dwStructSize;
        [FieldOffset(4)]
        public IntPtr lpszSourceUrlName;
        [FieldOffset(8)]
        public IntPtr lpszLocalFileName;
        [FieldOffset(12)]
        public uint CacheEntryType;
        [FieldOffset(16)]
        public uint dwUseCount;
        [FieldOffset(20)]
        public uint dwHitRate;
        [FieldOffset(24)]
        public uint dwSizeLow;
        [FieldOffset(28)]
        public uint dwSizeHigh;
        [FieldOffset(32)]
        public System.Runtime.InteropServices.ComTypes.FILETIME LastModifiedTime;
        [FieldOffset(40)]
        public System.Runtime.InteropServices.ComTypes.FILETIME ExpireTime;
        [FieldOffset(48)]
        public System.Runtime.InteropServices.ComTypes.FILETIME LastAccessTime;
        [FieldOffset(56)]
        public System.Runtime.InteropServices.ComTypes.FILETIME LastSyncTime;
        [FieldOffset(64)]
        public IntPtr lpHeaderInfo;
        [FieldOffset(68)]
        public uint dwHeaderInfoSize;
        [FieldOffset(72)]
        public IntPtr lpszFileExtension;
        [FieldOffset(76)]
        public uint dwReserved;
        [FieldOffset(76)]
        public uint dwExemptDelta;
    }
    

提交回复
热议问题