【发布时间】:2019-10-14 23:39:02
【问题描述】:
我正在尝试让控制台应用程序在后台运行,并检查一个小时内是否没有创建新文件。现在我面临的问题是如何获取文件夹中最新文件的时间。
这是我尝试过的:
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);
}
因此,我得到了文件夹中的最后一个文件。此控制台应用程序是否在后台运行?
现在我可以获取文件夹中最新文件的日期时间。但是如果一个小时内文件夹中没有新文件,我该如何查找呢?
更新代码
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());
}
【问题讨论】:
-
如果您有一个问题“我的应用程序在后台运行吗?”,那么很可能答案是“否”。您应该将应用程序创建为后台服务。请对此进行一些研究。您可以从here 或here for .Net Core 开始。
标签: c# .net console console-application