我发现此代码位于: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();
}
}
答案 0 :(得分:1)
从示例中可以清楚地看出,您已经获取了代码,即fileList是FileInfo的数组。你必须在代码之前声明并填充该数组。:
DirectoryInfo Dir = new DirectoryInfo(DirectoryPath);
FileInfo[] FileList = Dir.GetFiles("*.*", SearchOption.AllDirectories);