0

我想使用以下代码清除 IE 中的所有 cookie:

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);
            }
        }
    }

但是当我运行时,出现一个带有内容的消息框:进程无法访问文件:C:\User...\Microsoft\Windows\Temporary InterNet Files\counter.dat' 因为它正在被另一个进程使用 我该怎么办解决这个问题???

4

1 回答 1

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 的 File.Delete 文档,了解有关此方法可能引发的不同类型错误的更多信息。

于 2013-07-06T13:37:42.700 回答