大家知道,在我們訪問一個網站的時候。系統會把這個網站上的圖片,動畫等內容全部緩存到Internet臨時文件夾中。 我們可以通過 <Drives>:\Documents and Settings\<user>\Local Settings\Temporary Internet Files訪問。但是可能我們都沒有想到,裡面的文件實際卻不同於我們系統中其他的文件夾和文件的關系。
[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 FindNextUrlCacheEntry( IntPtr hEnumHandle, IntPtr lpNextCacheEntryInfo, ref int lpdwNextCacheEntryInfoBufferSize);
[DllImport("wininet.dll")] public static extern bool FindCloseUrlCache( IntPtr hEnumHandle);
引入以上三個函數來遍歷臨時文件夾,然後再引用
[DllImport("kernel32.dll",SetLastError=true, CharSet=CharSet.Auto)] public static extern int FileTimeToSystemTime( IntPtr lpFileTime, IntPtr lpSystemTime);
用來把 FileTime時間格式轉化成c#中的string類型,以便我們進一步操作。
主體程序如下:
#region 引入dll
[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; }
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)] public struct SYSTEMTIME { public short wYear; public short wMonth; public short wDayOfWeek; public short wDay; public short wHour; public short wMinute; public short wSecond; public short wMilliseconds; }
[DllImport("kernel32.dll",SetLastError=true, CharSet=CharSet.Auto)] public static extern int FileTimeToSystemTime( IntPtr lpFileTime, IntPtr lpSystemTime);
[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 FindNextUrlCacheEntry( IntPtr hEnumHandle, IntPtr lpNextCacheEntryInfo, ref int lpdwNextCacheEntryInfoBufferSize);
[DllImport("wininet.dll")] public static extern bool FindCloseUrlCache( IntPtr hEnumHandle);