Exemple #1
0
        public static WebBrowserCacheEntry[] FindCacheFilesForUrl(string url)
        {
            const int ERROR_NO_MORE_ITEMS = 259;             // No more items have been found.

            // Local variables
            int    cacheEntryInfoBufferSizeInitial = 0;
            int    cacheEntryInfoBufferSize        = 0;
            IntPtr cacheEntryInfoBuffer            = IntPtr.Zero;
            INTERNET_CACHE_ENTRY_INFOA internetCacheEntry;
            IntPtr enumHandle  = IntPtr.Zero;
            bool   returnValue = false;

            System.Collections.Generic.List <WebBrowserCacheEntry> wbcEntries = new System.Collections.Generic.List <WebBrowserCacheEntry>();
            try
            {
                enumHandle = FindFirstUrlCacheEntry(null, IntPtr.Zero, ref cacheEntryInfoBufferSizeInitial);
                if (enumHandle != IntPtr.Zero && ERROR_NO_MORE_ITEMS == Marshal.GetLastWin32Error())
                {
                    return(wbcEntries.ToArray());
                }

                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));
                    WebBrowserCacheEntry wbcEntry = new WebBrowserCacheEntry(internetCacheEntry);
                    if (wbcEntry.SourceUrl == new Uri(url))
                    {
                        wbcEntries.Add(wbcEntry);
                    }

                    cacheEntryInfoBufferSizeInitial = cacheEntryInfoBufferSize;
                    returnValue = FindNextUrlCacheEntry(enumHandle, cacheEntryInfoBuffer, ref cacheEntryInfoBufferSizeInitial);
                    if (!returnValue && ERROR_NO_MORE_ITEMS == Marshal.GetLastWin32Error())
                    {
                        Console.WriteLine(new System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error()).Message);
                        break;
                    }

                    if (!returnValue && cacheEntryInfoBufferSizeInitial > cacheEntryInfoBufferSize)
                    {
                        cacheEntryInfoBufferSize = cacheEntryInfoBufferSizeInitial;
                        cacheEntryInfoBuffer     = Marshal.ReAllocHGlobal(cacheEntryInfoBuffer, (IntPtr)cacheEntryInfoBufferSize);
                        returnValue = FindNextUrlCacheEntry(enumHandle, cacheEntryInfoBuffer, ref cacheEntryInfoBufferSizeInitial);
                        if (!returnValue)
                        {
                            Console.WriteLine(new System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error()).Message);
                        }
                    }
                }
                Marshal.FreeHGlobal(cacheEntryInfoBuffer);
            } catch (Exception e)
            {
                throw new InvalidOperationException("error reading cache", e);
            }
            return(wbcEntries.ToArray());
        }
        public static WebBrowserCacheEntry[] FindCacheFilesForUrl( string url )
        {
            const int ERROR_NO_MORE_ITEMS = 259; // No more items have been found.

            // Local variables
            int cacheEntryInfoBufferSizeInitial = 0;
            int cacheEntryInfoBufferSize = 0;
            IntPtr cacheEntryInfoBuffer = IntPtr.Zero;
            INTERNET_CACHE_ENTRY_INFOA internetCacheEntry;
            IntPtr enumHandle = IntPtr.Zero;
            bool returnValue = false;
            System.Collections.Generic.List<WebBrowserCacheEntry> wbcEntries = new System.Collections.Generic.List<WebBrowserCacheEntry>();
            try
            {
                enumHandle = FindFirstUrlCacheEntry( null, IntPtr.Zero, ref cacheEntryInfoBufferSizeInitial );
                if( enumHandle != IntPtr.Zero && ERROR_NO_MORE_ITEMS == Marshal.GetLastWin32Error() )
                    return wbcEntries.ToArray();

                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 ) );
                    WebBrowserCacheEntry wbcEntry = new WebBrowserCacheEntry( internetCacheEntry );
                    if( wbcEntry.SourceUrl == new Uri( url ) )
                        wbcEntries.Add( wbcEntry );

                    cacheEntryInfoBufferSizeInitial = cacheEntryInfoBufferSize;
                    returnValue = FindNextUrlCacheEntry( enumHandle, cacheEntryInfoBuffer, ref cacheEntryInfoBufferSizeInitial );
                    if( !returnValue && ERROR_NO_MORE_ITEMS == Marshal.GetLastWin32Error() )
                    {
                        Console.WriteLine( new System.ComponentModel.Win32Exception( Marshal.GetLastWin32Error() ).Message );
                        break;
                    }

                    if( !returnValue && cacheEntryInfoBufferSizeInitial > cacheEntryInfoBufferSize )
                    {
                        cacheEntryInfoBufferSize = cacheEntryInfoBufferSizeInitial;
                        cacheEntryInfoBuffer = Marshal.ReAllocHGlobal( cacheEntryInfoBuffer, (IntPtr) cacheEntryInfoBufferSize );
                        returnValue = FindNextUrlCacheEntry( enumHandle, cacheEntryInfoBuffer, ref cacheEntryInfoBufferSizeInitial );
                        if( !returnValue )
                            Console.WriteLine( new System.ComponentModel.Win32Exception( Marshal.GetLastWin32Error() ).Message );
                    }
                }
                Marshal.FreeHGlobal( cacheEntryInfoBuffer );
            } catch( Exception e )
            {
                throw new InvalidOperationException( "error reading cache", e );
            }
            return wbcEntries.ToArray();
        }