2013-07-06 76 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