zoukankan      html  css  js  c++  java
  • [转]c# 根据URL定位InternetCache中文件的位置

    本文转自:http://zeze-love.blogspot.com/2008/10/c-urlinternetcache.html

    从IE浏览器缓存中根据URL定位本地文件有很多用处,比如直接从缓存中保存浏览过的图片,避免再次下载以节省带宽,另外在显示或保存验证码图片之类每次访问都会随机改变的文件时也很有用。但是由于在本地系统中浏览器缓存的临时文件并不是用一般方式保存的(具体参加MSDN),所以处理上稍微麻烦一些。

    前段时间用 c# 写一个小程序时,使用如下代码获取指定URL文件缓存在本地文件系统中的临时文件的路径。
    using System.Runtime.InteropServices;

    ...


    [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文件的缓存在本地文件系统中的路径
    private 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);
    }
    }

  • 相关阅读:
    [项目管理]记一次外包过程遇到的“问题”以及“应对之道”
    [ZT]Web Standard and ASP.NET – Part1 XHTML Quick Start
    [前端技术]利用 try...catch 来跳出JQuery.each()
    [ZT]Use JQuery to adjust the iframe height
    [CSharp]复合格式化(Composite Formatting)
    [项目管理]关于项目的工期控制
    [CSharp]判断表达式为空的二元运算符
    MySoft.Data ORM组件之获取插入后的自增主键
    [前端技术]让iframe背景透明起来
    NSRunLoop
  • 原文地址:https://www.cnblogs.com/freeliver54/p/1326152.html
Copyright © 2011-2022 走看看