2013-07-06 77 views
0

我要清除所有Cookie在我的IE瀏覽器與下面的代碼:錯誤清除當瀏覽器cookie C#

public void ClearCookie() 
    { 
     string[] Cookies = 
      System.IO.Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.InternetCache)); 
     foreach (string currentFile in Cookies) 
     { 
      try 
      { 
       System.IO.File.Delete(currentFile); 
      } 

      catch (Exception ex) 
      { 
       MessageBox.Show(ex.Message); 
      } 
     } 
    } 

但是當我運行,一個消息框,與內容appeares:該進程無法訪問該文件:C: \ User ... \ Microsoft \ Windows \ Temporary InterNet Files \ counter.dat',因爲它正在被另一個進程使用 我應該怎麼做才能解決這個問題?

+0

關閉瀏覽器 –

回答

0

而是捕捉一般例外的,你可以專注於指定確切的錯誤類型,如:

 try 
     { 
      File.Delete(currentFile); 
     } 
     catch (IOException ex) 
     { 
      // file is locked, in use or has an open handle in another application 
      // so skip it 
     } 
     catch (UnauthorizedAccessException ex) 
     { 
      // you don't have permissions to delete the file 
     } 

這應該給你如何處理可能出現的文件IO異常多樣性的更好的指標。此外,請查看MSDN's File.Delete documentations瞭解此方法可能拋出的不同類型錯誤的更多信息。

+0

謝謝你,這是固定的。但是在我清除Cookie後,我登錄到網站,並且密碼仍然是自動填充的。我不知道cookie是否還沒有被刪除? – vyclarks

+0

不應將憑據存儲在Cookie中,但大多數情況下都以不同的,更安全的方式存儲在每個瀏覽器中。因此,刪除臨時Internet文件與清除整個瀏覽器的歷史記錄不同。 –

+0

哦,真的,你能告訴我如何通過代碼清除所有歷史記錄,實際上我不知道它 – vyclarks