0

I want to clear IE cache for a site using C# . I am having a Windows application which uses WebBrowser control to login to specific site. But, I am not storing any cache using my program , IE does it by default. I was trying to understand

https://msdn.microsoft.com/en-us/library/windows/desktop/aa384026(v=vs.85).aspx https://msdn.microsoft.com/en-us/library/windows/desktop/aa384049(v=vs.85).aspx https://msdn.microsoft.com/en-us/library/windows/desktop/aa383983(v=vs.85).aspx

But I couldn't find a solution out of it.

4
  • You can set headers on the page to prevent it from being cached: stackoverflow.com/a/8155121/5894241 Commented Aug 20, 2017 at 4:33
  • Possible duplicate of How to programmatically empty browser cache? Commented Aug 20, 2017 at 4:34
  • Many of the users are already using my application. For future I can use cache control , but my application loads different Site which i cannot control and it has login. Commented Aug 20, 2017 at 4:44
  • This Answer has a detailed info regarding cache-control. Commented Aug 20, 2017 at 7:04

1 Answer 1

2

you can Use my class to Clear IE Cache

public static class WebBrowserHelper
{
    #region WINAPI        
    [DllImport("wininet",  EntryPoint = "DeleteUrlCacheEntryA",  SetLastError = true)]
    public static extern bool DeleteUrlCacheEntry(IntPtr lpszUrlName);

    [DllImport("wininet", SetLastError = true)]
    public static extern bool DeleteUrlCacheGroup(long GroupId, int dwFlags, IntPtr lpReserved);

    [DllImport("wininet",  EntryPoint = "FindFirstUrlCacheEntryA",  SetLastError = true)]
    public static extern IntPtr FindFirstUrlCacheEntry(string lpszUrlSearchPattern, IntPtr lpFirstCacheEntryInfo, ref int lpdwFirstCacheEntryInfoBufferSize);

    [DllImport("wininet",  SetLastError = true)]
    public static extern IntPtr FindFirstUrlCacheGroup(int dwFlags, int dwFilter, IntPtr lpSearchCondition, int dwSearchCondition, ref long lpGroupId, IntPtr lpReserved);

    [DllImport("wininet", EntryPoint = "FindNextUrlCacheEntryA",  SetLastError = true)]
    public static extern bool FindNextUrlCacheEntry(IntPtr hFind, IntPtr lpNextCacheEntryInfo, ref int lpdwNextCacheEntryInfoBufferSize);

    [DllImport("wininet",  SetLastError = true)]
    public static extern bool FindNextUrlCacheGroup(IntPtr hFind, ref long lpGroupId, IntPtr lpReserved);
    #endregion

    [StructLayout(LayoutKind.Explicit)]
    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;
    }
    public static void ClearCache()
    {
        bool flag;
        bool flag1;
        long num = (long)0;
        int num1 = 0;
        int num2 = 0;
        IntPtr zero = IntPtr.Zero;
        IntPtr intPtr = IntPtr.Zero;
        bool flag2 = false;
        intPtr = FindFirstUrlCacheGroup(0, 0, IntPtr.Zero, 0, ref num, IntPtr.Zero);
        if ((intPtr == IntPtr.Zero ? true : 259 != Marshal.GetLastWin32Error()))
        {
            while (true)
            {
                flag = true;
                if ((259 == Marshal.GetLastWin32Error() ? false : 2 != Marshal.GetLastWin32Error()))
                {
                    flag2 = DeleteUrlCacheGroup(num, 2, IntPtr.Zero);
                    if ((flag2 ? false : 2 == Marshal.GetLastWin32Error()))
                    {
                        flag2 = FindNextUrlCacheGroup(intPtr, ref num, IntPtr.Zero);
                    }
                    if (flag2)
                    {
                        flag1 = true;
                    }
                    else
                    {
                        flag1 = (259 == Marshal.GetLastWin32Error() ? false : 2 != Marshal.GetLastWin32Error());
                    }
                    if (!flag1)
                    {
                        break;
                    }
                }
                else
                {
                    break;
                }
            }
            intPtr = FindFirstUrlCacheEntry(null, IntPtr.Zero, ref num1);
            if ((intPtr == IntPtr.Zero ? true : 259 != Marshal.GetLastWin32Error()))
            {
                num2 = num1;
                zero = Marshal.AllocHGlobal(num2);
                intPtr = FindFirstUrlCacheEntry(null, zero, ref num1);
                while (true)
                {
                    flag = true;
                    INTERNET_CACHE_ENTRY_INFOA structure = (INTERNET_CACHE_ENTRY_INFOA)Marshal.PtrToStructure(zero, typeof(INTERNET_CACHE_ENTRY_INFOA));
                    if (259 != Marshal.GetLastWin32Error())
                    {
                        num1 = num2;
                        flag2 = DeleteUrlCacheEntry(structure.lpszSourceUrlName);
                        if (!flag2)
                        {
                            flag2 = FindNextUrlCacheEntry(intPtr, zero, ref num1);
                        }
                        if (!(flag2 ? true : 259 != Marshal.GetLastWin32Error()))
                        {
                            break;
                        }
                        else if ((flag2 ? false : num1 > num2))
                        {
                            num2 = num1;
                            zero = Marshal.ReAllocHGlobal(zero, (IntPtr)num2);
                            flag2 = FindNextUrlCacheEntry(intPtr, zero, ref num1);
                        }
                    }
                    else
                    {
                        break;
                    }
                }
                Marshal.FreeHGlobal(zero);
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. But, how do I delete cache for a specific site?
this will clear all cache of your IE, if you want to clear cache for a specific site call DeleteUrlCacheEntry url, it will Delete cache of specific url

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.