用 C# 擷取 IE 臨時檔案

來源:互聯網
上載者:User

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

該文章轉載自:中國公司資訊線上《資訊之舟》“電腦之家”“編程愛好者”欄目,文由網友提供,著作權歸原作者所有,感謝您的使用!歡迎您再來!:http://www.kepusoft.com/soft/info/4102.html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.