首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> ASP.NET 基礎 >> 關於ASP.NET

呼叫 WinINET API 取得 IE 快取檔案實體路徑

     編輯:關於ASP.NET

透過 P/Invoke 呼叫 wininet.dll 的 FindFirstUrlCacheEntry 與 GetUrlCacheEntryInfo,可解析 URL 對應的本地快取檔案路徑。

核心結構與宣告

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct INTERNET_CACHE_ENTRY_INFO {
    public int dwStructSize;
    public IntPtr lpszSourceUrlName;
    public IntPtr lpszLocalFileName;
    public int CacheEntryType;
    public int dwUseCount;
    public int dwHitRate;
    public int dwSizeLow;
    public int dwSizeHigh;
    public FILETIME LastModifiedTime;
    public FILETIME ExpireTime;
    public FILETIME LastAccessTime;
    public FILETIME LastSyncTime;
    public IntPtr lpHeaderInfo;
    public int dwHeaderInfoSize;
    public IntPtr lpszFileExtension;
    public int dwExemptDelta;
}

[DllImport("wininet.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern IntPtr FindFirstUrlCacheEntry(
    [MarshalAs(UnmanagedType.LPTStr)] string lpszUrlSearchPattern,
    IntPtr lpFirstCacheEntryInfo,
    ref int lpdwFirstCacheEntryInfoBufferSize);

[DllImport("wininet.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern bool GetUrlCacheEntryInfo(
    [MarshalAs(UnmanagedType.LPTStr)] string lpszUrlName,
    IntPtr lpCacheEntryInfo,
    ref int lpdwCacheEntryInfoBufferSize);

取得快取檔案路徑函數

public static string GetCacheFileName(string url) {
    int needed = 0, bufSize;
    IntPtr buf;
    FindFirstUrlCacheEntry(null, IntPtr.Zero, ref needed);
    bufSize = needed;
    buf = Marshal.AllocHGlobal(bufSize);
    GetUrlCacheEntryInfo(url, buf, ref needed);
    var cacheItem = (INTERNET_CACHE_ENTRY_INFO)Marshal.PtrToStructure(buf, typeof(INTERNET_CACHE_ENTRY_INFO));
    string path = Marshal.PtrToStringAuto(cacheItem.lpszLocalFileName);
    Marshal.FreeHGlobal(buf);
    return path;
}

應用場景

  • 離線讀取已快取的圖片/頁面內容。
  • 清理特定 URL 的快取檔案。

注意:需引用 System.Runtime.InteropServices,且僅適用於 IE/WinINET 快取體系。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved