大家知道,在我们访问一个网站的时候。系统会把这个网站上的图片,动画等内容全部缓存到Internet临时文件夹中。
我们可以通过 <Drives>:/Documents and Settings/<user>/Local Settings/Temporary Internet Files访问。但是可能我们都没有想到,里面的文件实际却不同于我们系统中其他的文件夹和文件的关系。

举例说明,我们在VS.net下写一个函数来返回指定文件夹中的文件夹和所有文件时,但我们把Internet临时文件夹的地址传进去时,系统只会返回一个文件,那就是desktop.ini(每个文件夹都有),还有一个隐藏的文件夹。所以这就证明了在临时文件夹中的文件并不是按照普通的文件夹与文件的方式存在的。

其实windows是把临时文件全部存在一个隐藏的文件夹中,这个文件夹是我们都看不到的,然后靠一个index.dat的索引把内容全部读出来回显给用户。

那我们怎么用程序来读取其中的内容呢? 
首先要引用一个user.dll,在系统文件夹中。然后利用它其中的一些函数就可以遍历整个文件夹,并获得其中每个文件的信息。

[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);

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

C# 获取 IE 临时文件相关推荐

  1. java获取该临时文件的Path File.createTempFile(script, .sh);

    使用 File.createTempFile("script", ".sh") 可以在系统的临时目录中创建一个名为 script.sh 的临时文件.可以使用该文 ...

  2. java获取tomcat临时文件夹路径

    /** 生成execl文件路径,tomcat临时文件夹temp  **/ String path = request.getSession().getServletContext().getRealP ...

  3. 获取用户临时文件夹路径

    对文件操作,需要将文件临时存储在当前用户临时文件夹中: class Bt{public void LocalTempPath(){var tempPath = System.IO.Path.GetTe ...

  4. C# 如何获取用户临时文件夹路径

    System.IO.Path.GetTempPath();

  5. C# 系统应用之清除Cookies、IE临时文件、历史记录 转载

    http://blog.csdn.net/Eastmount/article/details/18821221 本文主要是项目"个人电脑使用记录清除软件"系类文章中关于清除浏览器C ...

  6. C# 系统应用之清除Cookies、IE临时文件、历史记录

    本文主要是项目"个人电脑使用记录清除软件"系类文章中关于清除浏览器Cookies.IE临时文件.最近使用历史记录等内容.该篇文章的基本思想路线是首先了解上网历史记录的Windows ...

  7. C# Windows获取系统路径汇总

    获取操作系统路径汇总(红色为常用) string dir = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) ...

  8. C#通过Windows API捕获窗,获取窗口文本(FindWindow、GetWindowText),附录:Windows窗口消息大全、Windows API大全

    文章目录 一.前言 二.使用Spy++工具分析窗口 三.C#通过Windows API捕获窗口,获取窗口文本 四.附录:Windows窗口消息 五.Windows API大全 1.API之网络函数 2 ...

  9. Windows API函数大全

    1. API之网络函数 WNetAddConnection 创建同一个网络资源的永久性连接 WNetAddConnection2 创建同一个网络资源的连接 WNetAddConnection3 创建同 ...

最新文章

  1. LDAPit's usage
  2. Python地信专题 | 基于geopandas的空间数据分析—数据结构篇
  3. [转] 爱情的隐式马尔可夫模型(Love in the Hidden Markov Model)
  4. 2017.10.25
  5. python内嵌函数和闭包与java 匿名内部类_Lambda表达式与匿名内部类的联系和区别...
  6. android texturevideoview 缓存,Android TextureView与VideoView性能
  7. kernel编译报错问题kernel is not clean, please run 'make mrproper'
  8. 28th Dec, 2012 我自己的问题
  9. CUDA核函数share memory
  10. MindManager2018,修改下载时间
  11. 模式识别的常用英文总结
  12. 路由器刷OpenWRT实现动态dns
  13. 轻量而敏捷的工业组态软件UI设计工具-ConPipe Studio 2022
  14. 使用Spark SQL读取Hive上的数据
  15. scrapy的分页(翻页处理)
  16. JPA 概述及 SpringDataJpa 框架基本使用指南
  17. win7桌面图标全变成windows media center 解决办法
  18. 试试在transformers中调用ERNIE
  19. python找素因子_python 素因子分解
  20. LSM-Tree(BigTable的理论模型)(转)

热门文章

  1. Python Qt GUI设计:窗口布局管理方法【基础】(基础篇—5)
  2. JS中编写函数去除HTML标签,js函数获取html中className所在的内容并去除标签
  3. python request.get()_使用Python request.get解析无法一次加载的html代码
  4. Ubuntu 14.04 64bit上安装LNMP环境
  5. Ubuntu 12.04 64bit上安装Apache Traffic Server 4.1.2
  6. Go 分布式学习利器(11)-- Go语言通过单链表 实现队列
  7. 初步判断内存泄漏方法
  8. centos安装pg以及pg配置ssl
  9. Milking Cows 挤牛奶
  10. 75. Find Peak Element 【medium】