码迷,mamicode.com
首页 > Windows程序 > 详细

C# 读取IE缓存文件(1)

时间:2014-12-15 13:25:32      阅读:636      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   ar   color   os   sp   for   

using System;
using System.Runtime.InteropServices;

namespace Common
{
  public  class IECache
    {
        [DllImport("Wininet.dll", SetLastError = true, CharSet = CharSet.Auto)]
        public static extern Boolean GetUrlCacheEntryInfo(String lpxaUrlName, IntPtr lpCacheEntryInfo, ref int lpdwCacheEntryInfoBufferSize);
        const int ERROR_FILE_NOT_FOUND = 0x2;
        struct LPINTERNET_CACHE_ENTRY_INFO
        {
            public int dwStructSize;
            IntPtr lpszSourceUrlName;
            public IntPtr lpszLocalFileName;
            int CacheEntryType;
            int dwUseCount;
            int dwHitRate;
            int dwSizeLow;
            int dwSizeHigh;
            FILETIME LastModifiedTime;
            FILETIME Expiretime;
            FILETIME LastAccessTime;
            FILETIME LastSyncTime;
            IntPtr lpHeaderInfo;
            int dwheaderInfoSize;
            IntPtr lpszFileExtension;
            int dwEemptDelta;
        }
        // 返回 指定URL文件的缓存在本地文件系统中的路径
        public string GetPathForCachedFile(string fileUrl)
        {
            int cacheEntryInfoBufferSize = 0;
            IntPtr cacheEntryInfoBuffer = IntPtr.Zero;
            int lastError; Boolean result;
            try
            {
                result = GetUrlCacheEntryInfo(fileUrl, IntPtr.Zero, ref cacheEntryInfoBufferSize);
                lastError = Marshal.GetLastWin32Error();
                if (result == false)
                {
                    if (lastError == ERROR_FILE_NOT_FOUND) 
                        return null;
                }
                cacheEntryInfoBuffer = Marshal.AllocHGlobal(cacheEntryInfoBufferSize);
                result = GetUrlCacheEntryInfo(fileUrl, cacheEntryInfoBuffer, ref cacheEntryInfoBufferSize);
                lastError = Marshal.GetLastWin32Error();
                if (result == true)
                {
                    Object strObj = Marshal.PtrToStructure(cacheEntryInfoBuffer, typeof(LPINTERNET_CACHE_ENTRY_INFO));
                    LPINTERNET_CACHE_ENTRY_INFO internetCacheEntry = (LPINTERNET_CACHE_ENTRY_INFO)strObj;
                    String localFileName = Marshal.PtrToStringAuto(internetCacheEntry.lpszLocalFileName); return localFileName;
                }
                else 
                    return null;// file not found
            }
            finally
            {
                if (!cacheEntryInfoBuffer.Equals(IntPtr.Zero)) 
                    Marshal.FreeHGlobal(cacheEntryInfoBuffer);
            }
        }
    }
}

只能获取 类似 http://www.baidu.com/img/baidu_jgylogo3.gif 这样的图片 

C# 读取IE缓存文件(1)

标签:style   blog   http   io   ar   color   os   sp   for   

原文地址:http://www.cnblogs.com/simadi/p/4164512.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!