c# webbrowser 清除cookie和緩衝

來源:互聯網
上載者:User

標籤: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();
}
}
這樣應該可以刪。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.