轉
C# 清除Cookies
現在越來越多的網站使用了Cookie技術,它給我們帶來友善的同時,也給我們帶來了一點小麻煩。 比如Gmail的自動登入。 在網上沒有找到合适的代碼,于是就自己寫了一個清除Cookie的函數(包括一個重載函數)。
CleanCookies方法
public bool CleanCookies(string URLStr, int ExType)
清除指定或全部Cookie資料
傳回值
類型:BOOL
用于判斷操作是否完成。
true表示成功 false表示失敗
參 數
URLStr
類型:string
指定要删除的網址Cookies,賦空時,删除所有Cookies資料
ExType
類型:int
指定浏覽器的類型
0為Internet Explorer 1為FireFox
重 載
public bool CleanCookies()
清除所有Cookie 預設IE浏覽器
範 例
清除網站http://mail.google.com/的Cookie資料 使用IE浏覽器
CleanCookies("mail.google.com",0);
清除網站http://mail.google.com/的Cookie資料 使用FireFox浏覽器
CleanCookies("mail.google.com",1);
清除全部Cookie資料 使用IE浏覽器
CleanCookies("",0);
使用重載函數删除所有Cookie資料
CleanCookies();
程 序
//清空126網站的Cookie資料
if (CleanCookies("www.126.com", 0))
MessageBox.Show("清除成功");
else
MessageBox.Show("清除失敗");
源代碼
public bool CleanCookies(string URLStr, int ExType)
{
//定義變量
string CookiesPath, FindDirctroy, OsTypeStr;
string UserProfile;
string XPCookiesPath, VistaCookiesPath;
long OsType;
//擷取使用者配置路徑
UserProfile = Environment.GetEnvironmentVariable("USERPROFILE");
MessageBox.Show(UserProfile);
//擷取作業系統類型
OsType = Environment.OSVersion.Version.Major;
//解析位址
if (URLStr == "")
FindDirctroy = "";
else
{
//用"."分割字元
char[] separator = { '.' };
string[] MyWords;
MyWords = URLStr.Split(separator);
//選取其中的關鍵字
try
{
FindDirctroy = MyWords[1];
}
catch
{
FindDirctroy = "";
//如果出錯提示
MessageBox.Show("輸入的網址格式不正确。");
}
//測試使用
MessageBox.Show(FindDirctroy);
}
//判斷浏覽器類型
if(ExType==0)
{
//IE浏覽器
XPCookiesPath = @"\Cookies\";
VistaCookiesPath = @"\AppData\Roaming\Microsoft\Windows\Cookies\";
}
else if(ExType == 1)
{
//FireFox浏覽器
XPCookiesPath = @"\Application Data\Mozilla\Firefox\Profiles\";
VistaCookiesPath = @"\AppData\Roaming\Mozilla\Firefox\Profiles\";
FindDirctroy = "cookies";
}
else
{
XPCookiesPath = "";
VistaCookiesPath = "";
return false;
}
//判斷作業系統類型
if (OsType == 5)
{
//系統為XP
OsTypeStr = "Microsoft Windows XP";
CookiesPath = UserProfile + XPCookiesPath;
//測試使用
MessageBox.Show(CookiesPath);
}
else if (OsType == 6)
{
//系統為Vista
OsTypeStr = "Microsoft Windows Vista";
CookiesPath = UserProfile + VistaCookiesPath;
//測試使用
MessageBox.Show(CookiesPath);
}
else if (OsType == 7)
{
//系統為Win 7
OsTypeStr = "Microsoft Windows 7";
CookiesPath = UserProfile + VistaCookiesPath;
//測試使用
MessageBox.Show(CookiesPath);
}
else
{
//未識别之作業系統
OsTypeStr = "Other OS Version";
CookiesPath = "";
return false;
}
//删除檔案
if (DeleteFileFunction(CookiesPath, FindDirctroy))
return true;
else
return false;
}
//重載函數
public bool CleanCookies()
{
if (CleanCookies("", 0))
return true;
else
return false;
}
private bool DeleteFileFunction(string filepath,string FindWords)
{
string Dstr;
//下面這些字串例外
string ExceptStr = "index.dat";
//解析删除關鍵字
if (FindWords == "")
Dstr = "*.*";
else
Dstr = "*" + FindWords + "*";
//删除cookie檔案夾
try
{
foreach (string dFileName in Directory.GetFiles(filepath,Dstr))
{
if (dFileName == filepath + ExceptStr)
continue;
File.Delete(dFileName);
}
}
catch (Exception e)
{
MessageBox.Show("Cookies删除失敗!\n" + e.ToString());
return false;
}
//深層周遊(解決Vista Low權限問題)
string[] LowPath = Directory.GetDirectories(filepath);
foreach (string ThePath in LowPath)
{
try
{
foreach (string dFileName in Directory.GetFiles(ThePath, Dstr))
{
if (dFileName == filepath + ExceptStr)
continue;
File.Delete(dFileName);
}
}
catch (Exception e)
{
MessageBox.Show("周遊檔案删除失敗!\n" + e.ToString());
return false;
}
}
//測試使用
MessageBox.Show("删除完成!");
return true;
}