日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

C# 获取 IE 临时文件

發布時間:2023/11/27 生活经验 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C# 获取 IE 临时文件 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
大家知道,在我們訪問一個網站的時候。系統會把這個網站上的圖片,動畫等內容全部緩存到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 临时文件的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。