c#net怎么实现删除创建时间最早的文件
展开全部
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace _12_文件创建时间
{
class Program
{
static void Main(string[] args)
{
string path = "D:\\";
string[] filePathArr = Directory.GetFiles(path, "*.txt", SearchOption.TopDirectoryOnly);
Dictionary<string, DateTime> fileCreateDate = new Dictionary<string, DateTime>();
for (int i = 0; i < filePathArr.Length; i++)
{
FileInfo fi = new FileInfo(filePathArr[i]);
fileCreateDate[filePathArr[i]] = fi.CreationTime;
}
fileCreateDate = fileCreateDate.OrderBy(f => f.Value).ToDictionary(f => f.Key, f => f.Value);
foreach (KeyValuePair<string, DateTime> item in fileCreateDate)
{
Console.WriteLine(item.Value + " -----> " + item.Key);
}
//File.Delete(fileCreateDate.First().Key);
Console.ReadKey();
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询