中國最大的web開發資源網站及技術社區,
大家知道,在我們訪問一個網站的時候。系統會把這個網站上的圖片,動畫等內容全部緩存到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