I'm trying to make a console app running in background and check if there's no new files created within an hour. Now I'm facing the problem how to get the time of the most recent file in a folder.
Here's what i have tried:
string path1 = @"C:\Users\nx011116\Documents\test folder\server";
string[] subdir1 = Directory.GetDirectories(path1);
for (int a = 0; a < subdir1.Length; a++)
{
var directory = new DirectoryInfo(subdir1[a]);
var myFile = directory.GetFiles()
.OrderByDescending(f => f.LastWriteTime)
.First();
Console.WriteLine(myFile);
}
As a result I'm getting the last file in the folder. Is this console app running in the background?
Now I'm able to get the DateTime of the most recent file in the folder. But how can I find if there's no new files in the folder within an hour?
Updated Code
string path1 = @"C:\Users\nx011116\Documents\test folder\server";
string[] subdir1 = Directory.GetDirectories(path1);
for (int a = 0; a < subdir1.Length; a++)
{
var directory = new DirectoryInfo(subdir1[a]);
var myFile = directory.GetFiles()
.OrderByDescending(f => f.LastWriteTime)
.First();
Console.WriteLine(myFile.LastAccessTime.ToString());
}