根据时间创建删除文件

时间:2013-01-02 10:28:04

标签: c# delete-file

我发现此代码位于:Get most recent N files and delete the rest

遇到错误:当前上下文中不存在名称'fileList'

如何设置我希望代码读取的文件夹目录?

var query = fileList.OrderByDescending(file => file.CreationTime);
var keepers = new List<FileInfo>();
var i = 0;
foreach (var file in query) {
    if (i++ < N)
    {
        keepers.Add(file);
    }
    else
    {
        file.Delete();
    }
}

1 个答案:

答案 0 :(得分:1)

从示例中可以清楚地看出,您已经获取了代码,即fileList是FileInfo的数组。你必须在代码之前声明并填充该数组。:

DirectoryInfo Dir = new DirectoryInfo(DirectoryPath);
FileInfo[] FileList = Dir.GetFiles("*.*", SearchOption.AllDirectories);