public static string GetPathForCachedFile(string fileUrl) { int cacheEntryInfoBufferSize = 0; IntPtr cacheEntryInfoBuffer = IntPtr.Zero; int lastError; Boolean result; try { // call to see how big the buffer needs to be result = GetUrlCacheEntryInfo(fileUrl, IntPtr.Zero, ref cacheEntryInfoBufferSize); lastError = Marshal.GetLastWin32Error(); if (result == false) { if (lastError == ERROR_FILE_NOT_FOUND) { return(null); } } // allocate the necessary amount of memory cacheEntryInfoBuffer = Marshal.AllocHGlobal(cacheEntryInfoBufferSize); // make call again with properly sized buffer 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; //INTERNET_CACHE_ENTRY_INFO internetCacheEntry = (INTERNET_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); } } }
// 返回 指定URL文件的缓存在本地文件系统中的路径 public static 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); // fileName not found } } finally { if (!cacheEntryInfoBuffer.Equals(IntPtr.Zero)) { Marshal.FreeHGlobal(cacheEntryInfoBuffer); } } }