国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

用 C# 獲取 IE 臨時文件

2024-07-21 02:19:47
字體:
來源:轉載
供稿:網友
中國最大的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



發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 廉江市| 五家渠市| 区。| 黔西| 阜城县| 唐海县| 韩城市| 平果县| 怀化市| 射阳县| 青冈县| 梁河县| 根河市| 玉门市| 武邑县| 延庆县| 宝山区| 台安县| 甘谷县| 通渭县| 新田县| 当阳市| 吉安县| 西乌珠穆沁旗| 淮安市| 前郭尔| 云浮市| 临沂市| 西华县| 马边| 德化县| 民丰县| 灌云县| 孟津县| 泾川县| 扎囊县| 庆安县| 玛多县| 霍邱县| 隆化县| 庆城县|