[转]用C#获取IE临时文件复制  [转]用C#获取IE临时文件保存
[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);

引入以上三个函数来遍历临时文件夹,然后再引用 
[转]用C#获取IE临时文件复制  [转]用C#获取IE临时文件保存
[dllimport("kernel32.dll", setlasterror = true, CharSet = CharSet.Auto)]
public static extern int FileTimeToSystemTime(
IntPtr lpFileTime,
IntPtr lpSystemTime);

用来把 FileTime时间格式转化成c#中的string类型,以便我们进一步操作。 

  主体程序如下: 
[转]用C#获取IE临时文件复制  [转]用C#获取IE临时文件保存
#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);
const int ERROR_NO_MORE_ITEMS = 259;

#endregion

#region FileTimeToSystemTime

private string FILETIMEtoDataTime(FILETIME time)
{
IntPtr filetime = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(FILETIME)));
IntPtr systime = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(SYSTEMTIME)));
Marshal.StructureToPtr(time, filetime, true);
FileTimeToSystemTime(filetime, systime);
SYSTEMTIME st = (SYSTEMTIME) Marshal.PtrToStructure(systime, typeof(SYSTEMTIME));
string Time = st.wYear.ToString() + "." + st.wMonth.ToString() + "." + st.wDay.ToString()
+ "." + st.wHour.ToString() + "." + st.wMinute.ToString() + "." + st.wSecond.ToString();
return Time;
}

#endregion

#region 加载数据
private void FileOk_Click(object sender, System.EventArgs e)
{
int nNeeded = 0, nBufSize;
IntPtr buf;
INTERNET_CACHE_ENTRY_INFO CacheItem;
IntPtr hEnum;
bool r;
findfirsturlcacheentry(null, IntPtr.Zero, ref nNeeded);
if (Marshal.GetLastWin32Error() == ERROR_NO_MORE_ITEMS)
return;
nbufsize = nNeeded;
buf = Marshal.AllocHGlobal(nBufSize);
hEnum = FindFirstUrlCacheEntry(null, buf, ref nNeeded);
while (true)
{
CacheItem = (INTERNET_CACHE_ENTRY_INFO) Marshal.PtrToStructure(buf,
typeof(INTERNET_CACHE_ENTRY_INFO));
string modifiedTime = FILETIMEtoDataTime(CacheItem.LastModifiedTime);
string expireTime = FILETIMEtoDataTime(CacheItem.ExpireTime);
string accessTime = FILETIMEtoDataTime(CacheItem.LastAccessTime);
string syncTime = FILETIMEtoDataTime(CacheItem.LastSyncTime);

#region 获得数据,存入数据库
        try
{
//此處遍歷CacheItem即可
            //例如
            string s = Marshal.PtrToStringAuto(CacheItem.lpszSourceUrlName);
}
catch
{
//異常處理
        }
        #endregion

string s = Marshal.PtrToStringAuto(CacheItem.lpszSourceUrlName);
nneeded = nBufSize;
r = FindNextUrlCacheEntry(hEnum, buf, ref nNeeded);
if (!r && Marshal.GetLastWin32Error() == ERROR_NO_MORE_ITEMS)
break;
if (!r && nNeeded > nBufSize)
{
nBufSize = nNeeded;
buf = Marshal.ReAllocHGlobal(buf, (IntPtr) nBufSize);
FindNextUrlCacheEntry(hEnum, buf, ref nNeeded);
}
}
MessageBox.Show("系统数据加载完毕!");
Marshal.FreeHGlobal(buf);
}

#endregion

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-15
  • 2021-11-12
  • 2021-12-17
  • 2022-12-23
猜你喜欢
  • 2021-10-24
  • 2021-04-11
  • 2022-12-23
  • 2021-05-18
  • 2022-12-23
  • 2022-12-23
  • 2021-12-28
相关资源
相似解决方案