Get IE temp files in C #

Source: Internet
Author: User
Tags bool filetime tostring
As you know, when we visit a website. The system caches images, animations, and so on on this site to the Temporary Internet folder.
We can access Settings\Temporary Internet files via <drives>:\documents and settings\<user>\local. But maybe we didn't realize that the files inside were actually different from the other folders and files in our system.

For example, we write a function under Vs.net to return the folder and all the files in the specified folder, but when we pass the address of the Temporary Internet folder, the system returns only one file, which is Desktop.ini (every folder has) and a hidden folder. So this proves that files in a temporary folder do not exist in the same way as normal folders and files.

In fact, Windows is a temporary file all exist in a hidden folder, this folder is we can not see, and then rely on a Index.dat index to read all the content back and forth to the user.

So how do we use the program to read the content? Because these days in helping students finish his graduation design, so studied a bit.
First, refer to a User.dll in the system folder. You can then use some of its functions to traverse the entire folder and get information about each of these files.

[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);


Introduce the above three functions to traverse the temporary folder and then refer to the

[DllImport ("kernel32.dll", Setlasterror=true, CharSet=CharSet.Auto)]
public static extern int FileTimeToSystemTime (
IntPtr Lpfiletime,
IntPtr lpsystemtime);

Used to convert the FILETIME time format into a string type in C # so that we can further manipulate it.

The main program is as follows:

#region Introducing DLLs

[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 Load Data
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 access to data, stored in a database
Try
{

This experience CacheItem can be
For example
string s = Marshal.ptrtostringauto (cacheitem.lpszsourceurlname);
}
Catch
{
Unusual processing
}
#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 ("System Data loading complete!") ");
Marshal.freehglobal (BUF);

}

#endregion





Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.