简体   繁体   English

无法从“最近”文件夹中删除文件

[英]Can not delete files from Recent folder

I am writing a Software that can delete Temporary files, Prefetch data, files in Recent folder and so on. 我正在编写一个可以删除临时文件,预取数据,“最近”文件夹中的文件等的软件。 My problem is I can delete files from Temp folder successfully, but when I try for Recent folder, an exception is thrown, "Access to path...is denied". 我的问题是我可以成功地从Temp文件夹中删除文件,但是当我尝试使用“最近”文件夹时,将引发异常,“拒绝访问路径...”。 PS: According to some other questions, I have set File attributes to normal, but still no luck. PS:根据其他一些问题,我已将File属性设置为normal,但仍然没有运气。 Please help me on this issue. 请帮我解决这个问题。 For your better understand, I put some code here: 为了让您更好地理解,我在此处添加了一些代码:

public Boolean CleanRecentData()
{
        isAllClean = true;
        String SysRecentPath = System.Environment.GetEnvironmentVariable("USERPROFILE") + "\\Recent";
        DirectoryInfo SysRecDir = new DirectoryInfo(SysRecentPath);
        File.SetAttributes(SysRecentPath, FileAttributes.Normal);

foreach (FileInfo fi in SysRecDir.GetFiles())   //Access Denied 
                                                 //Exception is thrown here
        {
            try
            {
                fi.Delete();
            }
            catch (Exception ex)
            {
                recentLogLines.AppendLine(ex.Message);
                isAllClean = false;
            }
        }

        foreach (DirectoryInfo dir in SysRecDir.GetDirectories())
        {
            try
            {
                dir.Delete(true);
            }
            catch (Exception ex)
            {
                recentLogLines.AppendLine(ex.Message);
                isAllClean = false;
            }
        }

        return isAllClean;
    }

Are you able to access the Recent folder via Windows Explorer? 您可以通过Windows资源管理器访问“最近”文件夹吗?

You could go ahead and change the permissions in your system, but NOT in your users systems. 您可以继续更改系统中的权限,但不能更改用户系统中的权限。 Therefore you could handle this exception condition in two ways. 因此,您可以通过两种方式处理此异常情况。

  1. You need to check if you have file access before accessing, using FileIOPermission but this might be redundant and wasteful if you are doing it on too many files. 您需要使用FileIOPermission在访问之前检查是否具有文件访问权限,但是如果要对太多文件进行访问,这可能是多余且浪费的。
  2. Just try to open the file and put your effort into a good exception handler if it fails Reference 只要尝试打开该文件,并把你的精力投入到了良好的异常处理程序,如果它失败参考

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM