標籤:c class a tar ext int
由於快取檔案是特殊的檔案,以及WebBrowser與IE版本有關
因此刪除緩衝絕對不可能用一些IO函數就總可以解決的
因此我的這些函數在IO操作的基礎上,又針對環境進行相應的清理。
static class WebCleaner
{
/*
* 7 個靜態函數
* 私人函數
* private bool FileDelete() : 刪除檔案
* private void FolderClear() : 清除檔案夾內的所有檔案
* private void RunCmd() : 運行內部命令
*
* 公有函數
* public void CleanCookie() : 刪除Cookie
* public void CleanHistory() : 刪除記錄
* public void CleanTempFiles() : 刪除臨時檔案
* public void CleanAll() : 刪除所有
*
*
*
* */
//private
///
/// 刪除一個檔案,System.IO.File.Delete()函數不可以刪除唯讀檔案,這個函數可以強行把唯讀檔案刪除。
///
/// 檔案路徑
/// 是否被刪除
static bool FileDelete(string path)
{
//first set the File\‘s ReadOnly to 0
//if EXP, restore its Attributes
System.IO.FileInfo file = new System.IO.FileInfo(path);
System.IO.FileAttributes att = 0;
bool attModified = false;
try
{
//### ATT_GETnSET
att = file.Attributes;
file.Attributes &= (~System.IO.FileAttributes.ReadOnly);
attModified = true;
file.Delete();
}
catch (Exception e)
{
if (attModified)
file.Attributes = att;
return false;
}
return true;
}
//public
///
/// 清除檔案夾
///
/// 檔案夾路徑
static void FolderClear(string path)
{
System.IO.DirectoryInfo diPath = new System.IO.DirectoryInfo(path);
foreach (System.IO.FileInfo fiCurrFile in diPath.GetFiles())
{
FileDelete(fiCurrFile.FullName);
}
foreach (System.IO.DirectoryInfo diSubFolder in diPath.GetDirectories())
{
FolderClear(diSubFolder.FullName); // Call recursively for all subfolders
}
}
static void RunCmd(string cmd)
{
System.Diagnostics.Process.Start(\"cmd.exe\", \"/c \" + cmd);
}
///
/// 刪除記錄
///
public static void CleanHistory()
{
string[] theFiles = System.IO.Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.History), \"*\", System.IO.SearchOption.AllDirectories);
foreach (string s in theFiles)
FileDelete(s);
RunCmd(\"RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 1\");
}
///
/// 刪除臨時檔案
///
public static void CleanTempFiles()
{
FolderClear(Environment.GetFolderPath(Environment.SpecialFolder.InternetCache));
RunCmd(\"RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 8\");
}
///
/// 刪除Cookie
///
public static void CleanCookie()
{
string[] theFiles = System.IO.Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.Cookies), \"*\", System.IO.SearchOption.AllDirectories);
foreach (string s in theFiles)
FileDelete(s);
RunCmd(\"RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 2\");
}
///
/// 刪除全部
///
public static void CleanAll()
{
CleanHistory();
CleanCookie();
CleanTempFiles();
}
}
這樣應該可以刪。