我试图通过写日期获取目录中所有文件的列表:
private void Form1_Load(object sender, EventArgs e) {
DateTime LastCreatedDate =
Properties.Settings.Default["LastDateTime"].ToDateTime();
string [] filePaths = Directory.GetFiles(@"\\Pontos\completed\", "*_*.csv")
.OrderBy(p => System.IO.File.GetLastWriteTime(p))
.Where(p>=LastCreatedDate);
}
问题
string []
不适合这个,因为它不知道如何进行转换。我应该使用哪种数据类型?答案 0 :(得分:2)
您是否有任何理由不使用DirectoryInfo
而不是Directory
- 这样可以节省您必须将文件路径解析回Files
以获取日期,或者存储日期在一个单独的变量中:
DateTime lastCreatedTime = new DateTime(2012, 01, 30, 05, 12, 00);
var files = new DirectoryInfo(@"\\Pontos\completed\").GetFiles("*_*.csv")
.Where(f => f.LastWriteTime >= lastCreatedTime)
.OrderBy(f => f.LastWriteTime)
.Select(f => new {f.FullName});
foreach (var file in files) {
Console.WriteLine(file.FullName);
}
答案 1 :(得分:1)
未经测试但应该有效:
private void Form1_Load(object sender, EventArgs e)
{
DateTime LastCreatedDate = Properties.Settings.Default["LastDateTime"].ToDateTime();
var filePaths = Directory.GetFiles(@"\\Pontos\completed\", "*_*.csv").Select(p => new {Path = p, Date = System.IO.File.GetLastWriteTime(p)})
.OrderBy(x=>x.Date)
.Where(x=>x.Date>=LastCreatedDate);
}
答案 2 :(得分:1)
你可以这样做:
Directory.GetFiles(@"\\Pontos\completed\", "*_*.csv")
.Select(p => new { FileName = p, Modified = System.IO.File.GetLastWriteTime(p) })
.Where(p => p.Modified > LastCreatedDate)
.OrderBy(p => p.Modified);