I am trying to get a list of all files in a directory by write date:
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);
}
Questions
string []
is not working for this one because it does not know how to do conversion. which data type should I be using?Is there any reason you're not using DirectoryInfo
instead of Directory
- this will save you having to parse the file paths back into Files
to get the dates, or store the dates in a separate variable:
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With