C # clearing Cookies, temporary IE files, and history of System Applications

Source: Internet
Author: User


This document describes how to delete a cache on the Internet of the project "personal computer use record cleanup software.

I. Path of the IE history file

Internet Explorer has a specified folder to store all the information recorded on the Internet, including IE cache files, Cookies files, recent browsing history, accessed URLs, address bar URLs, IE table/password records, and temporary files. before deleting the IE cache, we will briefly introduce the file paths of cookies, Temporary Internet Files, and IE history records.
1. in Windows, the Cookie is saved at "C: \ Users \ dell \ AppData \ Roaming \ Microsoft \ Windows \ Cookies ". cookie records information such as user ID, password, browser webpage, and stay time. as shown in:

2. in Windows, the Temporary Internet Files are stored in "C: \ Users \ dell \ AppData \ Local \ Microsoft \ Windows \ Temporary Internet Files ", it stores the content of recently browsed web pages (web pages, images, media copies, etc.) for fast query and speed improvement in the future.


3. in Windows, the path of the IE History is "C: \ Users \ dell \ AppData \ Local \ Microsoft \ Windows \ History". The historical records are the addresses of websites that have been visited recently, it is stored by time and site. as shown in:


In general, IE clears historical records, including download history, form data, password, ActiveX, and other data, such:

Ii. delete files using delete

PassEnvironment. GetFolderPath (Environment. SpecialFolder. InternetCache) obtains the Temporary Internet Files (Temporary Internet Files) and the. dat file path

// Get IE temporary files
private void button2_Click (object sender, EventArgs e)
{
    listBox1.Items.Clear ();
    string dirPath = Environment.GetFolderPath (Environment.SpecialFolder.InternetCache);
    this.textBox1.Text = dirPath.ToString ();
    DirectoryInfo dir = new DirectoryInfo (dirPath);
    // traverse all folders and display .dat files
    foreach (FileInfo file in dir.GetFiles ("*. jpg", SearchOption.AllDirectories))
    {
        try
        {
            listBox1.Items.Add (file.DirectoryName + "\\" + file);
            //file.Delete ();
        }
        catch (Exception msg) // Exception handling
        {
            MessageBox.Show (msg.Message);
        }
    }
}
Also refer to the namespace using System.IO; the results are as follows:


In this way, it seems that clearing all files in the folder realizes the function of clearing temporary Internet files, but using File.Delete (FileName) to delete files in the folder will encounter "The file is being used by another process, so the process cannot be accessed. This file is "or" file access denied. Because the Temporary Internet Files directory is a system folder, some files cannot be read.
(This resource is about viewing and deleting IE temporary files for everyone to learn http://download.csdn.net/detail/jee89731/3465688)

Three. Call rundll32.exe to achieve
Use RunDll32.exe to run the Internet option corresponding dll file to clear the IE cache (temporary Internet files, cookies, browser history, form records, user name and password, etc.).
<一> .rundll32.exe
The function of rundll32.exe (running a 32-bit dll file) is to execute the internal functions in the DLL file and call the dynamic link library through the command line. Where DLLname is the name of the DLL file to be executed, Functionname is the specific reference function to execute the DLL file, and [Arguments] is the specific parameter of the derived function).
Ctrl + R calls the run input command to execute, and can run the corresponding operation. For example, rundll32.exe shell32.dll, Control_RunDLL function is to execute the display control panel. The operation of clearing the IE cache is as follows:

//1.History (History)
RunDll32.exe InetCpl.cpl, ClearMyTracksByProcess 1
//2.Cookies
RunDll32.exe InetCpl.cpl, ClearMyTracksByProcess 2
//3.Temporary Internet Files (Temporary Internet Files)
RunDll32.exe InetCpl.cpl, ClearMyTracksByProcess 8
//4.Form. Data (form data)
RunDll32.exe InetCpl.cpl, ClearMyTracksByProcess 16
// 5. Passwords (passwords)
RunDll32.exe InetCpl.cpl, ClearMyTracksByProcess 32
// 6. Delete All (Delete All)
RunDll32.exe InetCpl.cpl, ClearMyTracksByProcess 255
//7.Delete All-"Also delete files and settings stored by add-ons"
RunDll32.exe InetCpl.cpl, ClearMyTracksByProcess 4351
<2> .ShellExecute

Use the Win32 API function ShellExecute () to execute the above command line to achieve the clear function. The function of ShellExecute is to run an external program or open a registered file, open a directory, print a file, etc., and have certain Control. Its function prototype and parameters are defined as follows:

// The ShellExecute function runs an external program and controls the program (the application handle is returned after successful execution)
static extern IntPtr ShellExecute (
    IntPtr hwnd, // Used to specify the parent window handle
    string lpOperation, // Used to specify the operation to be performed. Among them, open the file or folder specified by FileName.
    string lpFileName, // Used to specify the file name to be operated or the file name of the program to be executed
    string lpParameters, // Specify parameters for the program to be opened. If FileName is an executable program, this parameter specifies the command line parameters. Otherwise, the file is opened. The parameter is nil
    string lpDirectory, // default directory, used to specify the default directory
    ShowCommands nShowCmd // Open option. If the FileName parameter is an executable program, this parameter specifies the initial display method of the program window, otherwise this parameter is 0
);

// ShellExecute function ShowCmd parameter optional value
public enum ShowCommands: int
{
    SW_HIDE = 0, // Hide the window and activate another window (active)
    SW_SHOWNORMAL = 1, // Display window with original size and position, activate
    SW_NORMAL = 1, // Same as SW_SHOWNORMAL
    SW_SHOWMINIMIZED = 2, // Minimize the window, activate
    SW_SHOWMAXIMIZED = 3, // Maximize the window, activate
    SW_MAXIMIZE = 3, // Same as SW_SHOWMAXIMIZED
    SW_SHOWNOACTIVATE = 4, // Display with the latest size and position, without changing the active window (not activated)
    SW_SHOW = 5, // Same as SW_SHOWNORMAL
    SW_MINIMIZE = 6, // Minimize the window, do not activate
    SW_SHOWMINNOACTIVE = 7, // Same as SW_MINIMIZE
    SW_SHOWNA = 8, // Same as SW_SHOWNOACTIVATE
    SW_RESTORE = 9, // Same as SW_SHOWNORMAL
    SW_SHOWDEFAULT = 10, // Same as SW_SHOWNORMAL
    SW_FORCEMINIMIZE = 11, // Minimize the window
    SW_MAX = 11 // Same as SW_SHOWNORMAL
}
Use [DllImport ("shell32.dll")] to call the Win32 API (these APIs are program interfaces provided by Microsoft). After importing, you can call the class library where the API declaration form is located. The system file shell32.dll is stored in the Windows system file The important files in the folder are usually created during the installation of the operating system. The program source code is as follows:

// ShellExecute function ShowCmd parameter optional value
public enum ShowCommands: int
{
    SW_HIDE = 0,
    SW_SHOWNORMAL = 1,
    SW_NORMAL = 1,
    SW_SHOWMINIMIZED = 2,
    SW_SHOWMAXIMIZED = 3,
    SW_MAXIMIZE = 3,
    SW_SHOWNOACTIVATE = 4,
    SW_SHOW = 5,
    SW_MINIMIZE = 6,
    SW_SHOWMINNOACTIVE = 7,
    SW_SHOWNA = 8,
    SW_RESTORE = 9,
    SW_SHOWDEFAULT = 10,
    SW_FORCEMINIMIZE = 11,
    SW_MAX = 11
}

// Call Win32 API
[DllImport ("shell32.dll")]
static extern IntPtr ShellExecute (IntPtr hwnd, string lpOperation, string lpFile,
    string lpParameters, string lpDirectory, ShowCommands nShowCmd);

// Click button1 to delete IE temporary files
private void button1_Click (object sender, EventArgs e)
{
    ShellExecute (IntPtr.Zero, "open", "rundll32.exe",
        "InetCpl.cpl, ClearMyTracksByProcess 255", "", ShowCommands.SW_SHOWMAXIMIZED);
}
Among them, DllImport needs to refer to the named control using System.Runtime.InteropServices; at the same time, it runs RunDll32.exe InetCpl.cpl, ClearMyTracksByProcess 255 to delete all files. The IntPtr structure is used to indicate a specific type of pointer or handle, and IntPtr.Zero is initialized to a zero handle. Run result As shown:

Note: Although I learned the specific use method of ShellExecute, I still didn't understand the specific changes about the ShowCommands parameter; at the same time, there are too few books about the relevant aspects.

<3>. ShellExecute function extension
The ShellExecute () function also uses many functions to bring convenience to everyone. Here are a few simple usages. (Replace the button1 code above)

// Call rundll32.exe to clear history
ShellExecute (IntPtr.Zero, "open", "rundll32.exe",
    "InetCpl.cpl, ClearMyTracksByProcess 255", "", ShowCommands.SW_SHOWMAXIMIZED);
// Visit csdn eastmount blog
ShellExecute (this.Handle, "open", "http://blog.csdn.net/eastmount",
    null, null, ShowCommands.SW_SHOW);
// Call calc.exe to open the calculator
ShellExecute (this.Handle, "open", "calc.exe", null, null, ShowCommands.SW_FORCEMINIMIZE);
// Call NOTEPAD.EXE to open Notepad
ShellExecute (this.Handle, "open", "NOTEPAD.EXE", null,null, ShowCommands.SW_SHOWNORMAL);
<4>. Call Process to start RunDll32.exe

With the help of RunDll32, exe runs the Internet option to delete temporary files, you can also call Process to start RunDll32.exe. The following code is for reference only, I did not study this method, only as an online note for your own review:

// Clear ie cache, cookies and all records
private void IEclear ()
{
    Process process = new Process ();
    process.StartInfo.FileName = "RunDll32.exe";
    process.StartInfo.Arguments = "InetCpl.cpl, ClearMyTracksByProcess 255";
    process.StartInfo.UseShellExecute = false; // Close the use of Shell
    process.StartInfo.RedirectStandardInput = true; // Redirect standard input
    process.StartInfo.RedirectStandardOutput = true; // Redirect standard output
    process.StartInfo.RedirectStandardError = true; // Redirect error output
    process.StartInfo.CreateNoWindow = true;
    process.Start ();
}
4. Summary
This article mainly combines my own project to clear browser cookies, IE temporary files, recent history, the article shows my thoughts well:
1. First of all, the author thought of obtaining the path of the corresponding specified file, and the file under the path can be emptied to realize the function;
2. However, because the system files cannot be accessed or stopped, the author found the implementation with the help of RunDll32.exe, mainly about calling the Win32 API function ShellExecute to delete the IE cache;
3. Finally, the ShellExcute () function and other operations, and the code to call Process to start RunDll32.exe are added.
At the same time, in a series of articles, the author first talked about the use of Windows recent use history articles, cookies and sessions, and finally completed the article to clear the history operation (other methods are the same).
The article refers to Baidu Encyclopedia C # method of clearing IE temporary file cache cookies | xueer8835 article | zjw article | Adam Viki article | CSDN delete cookies discussion
Finally, I hope this article will be helpful to everyone, if there are errors or deficiencies, please Haihan!
(By: Eastmount 2014-1-28 1pm http://blog.csdn.net/eastmount)

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.