我正在尝试按写入日期获取目录中所有文件的列表:
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);
}
问题
- 如何正确执行 WHERE 子句以仅获取大于或等于我设置中的日期的文件?
string []
不适用于这个,因为它不知道如何进行转换。我应该使用哪种数据类型?
最佳答案
你有什么理由不使用 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);
}
关于c# - 尝试使用 LINQ 按创建日期获取目录中的文件列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9386558/