首页 ┆ 网站地图 ┆ 在线留言 ┆ 游戏资讯 ┆ 资源下载 ┆ 端午节祝福 ┆ 迅雷在线影视
设为首页
加入收藏
联系我们
高级搜索
您当前的位置: 主页>NET专区>C#语言>C#获取IE临时文件夹中的文件代码
C#获取IE临时文件夹中的文件代码
来源: 发布时间:2008-04-02 发布人: 浏览: 人次   字体: [ ]  
大家知道,在我们访问一个网站的时候。系统会把这个网站上的图片,动画等内容全部缓存到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

共2页: 上一页 1 [2] 下一页
相 关 文 章   发布商链接
·什么是c#深拷贝与浅拷贝及其代码示例
·c#访问修饰符介绍及其应用实例
·C#如何获取文件路径中的文件名?
·c#子线程控制进度条的一个简单例子
·什么是泛型编程思想?及其简单的应用...
·如何使用C#获取IIS服务器版本?
·如何在c#中去掉字符串中的回车符?
·C#如何从文本文件读取信息并使用Arra...
·什么是泛型?泛型的一些概述
·C#对timer类的使用操作代码
 §最新评论:(评论内容只代表网友观点,与本站立场无关!)
网名: 验证码:  【所有评论】【↑返回顶部
评 分: 12 345
评论内容:(不能超过500字,请自觉遵守互联网相关政策法规。[按 Ctrl+Enter 可直接提交]
注意:请勿在本站发布政治话题、色情及违反法律的内容。
IT知道网 声明:刊登此文章是为了传递更多信息,文章内容仅供参考,转载请注明出处。
推 荐 文 章
·有助快速理解C#委托和事件之...
·C#泛型学习笔记总结
·c#语言从dbf数据库提取数据并
·C#编写控制线程的运行和线程...
·怎样在C#中删除只读文件及其...
·C#函数中返回多个值的两种方...
·如何在C#中插入照片到Excel文
·如何在c#中获取事件注册的方...
·C#对timer类的使用操作代码
·什么是泛型?泛型的一些概述
·C#如何从文本文件读取信息并...
·如何在c#中去掉字符串中的回...
·如何使用C#获取IIS服务器版本...
·什么是泛型编程思想?及其简...
·c#子线程控制进度条的一个简...
热 门 文 章
·c#中收发邮件处理代码(POP3,...
·C#如何获取文件路径中的文件...
·什么是c#深拷贝与浅拷贝及其...
·C#中关于四舍五入函数的讨论...
·c#中的ArrayList属性祥解及其...
·C#语言实现创建、删除和移动...
·快速进行List排序的通用方法...
·C#对timer类的使用操作代码
·String.Split 方法使用及其代...
·如何在c#中去掉字符串中的回...
·用C#编写ActiveX控件代码实例
·C#语言实现从XML文件导出数据...
·C#如何从文本文件读取信息并...
·浅谈接口和抽象类的区别,加...
·c#子线程控制进度条的一个简...
网站首页 - 关于本站 - 加入收藏 - 网站地图 - 友情连接 - 在线留言 - 联系我们 - 返回顶部
Copyright © 2007 IT知道网.[冀ICP备07026896号]. All Rights Reserved .