总结C#清除IE临时文件缓存cookies的两个方法

作者:袖梨 2022-06-25

比如:
Temporary Internet Files  (Internet临时文件)
RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 8

Cookies
RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 2

History (历史记录)
RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 1

Form. Data (表单数据)
RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 16

Passwords (密码)
RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 32

Delete All  (全部删除)
RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 255

Delete All - "Also delete files and settings stored by add-ons"
RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 4351

我们在c#编程中如何调用这些指令呢?有2种方式,一是调用Process启动 RunDll32.exe,二是 调用Win32 API函数 ShellExecute 或者 CreateProcess 来执行以上的命令行就可以了。

方法一:调用Process启动 RunDll32.exe

private void IEclear()
{
    //清除ie缓存、cookie以及一切记录
    Process process = new Process();
    process.StartInfo.FileName = "RunDll32.exe";
    process.StartInfo.Arguments = "InetCpl.cpl,ClearMyTracksByProcess 255";
    process.StartInfo.UseShellExecute = false;
    process.StartInfo.RedirectStandardInput = true;
    process.StartInfo.RedirectStandardOutput = true;
    process.StartInfo.RedirectStandardError = true;
    process.StartInfo.CreateNoWindow = true;
    process.Start();
}

方法二: 调用Win32 API函数 ShellExecute 或者 CreateProcess 来执行以上的命令行就可以了。

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
}

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

//清除IE临时文件
ShellExecute(IntPtr.Zero, "open", "rundll32.exe", " InetCpl.cpl,ClearMyTracksByProcess 8", "", ShowCommands.SW_HIDE);

通过以上的方法就可以成功在软件中删除IE的cookies、缓存、临时文件等。

相关文章

精彩推荐