C# 搜索具有时间戳的文件
我的应用程序中有这样一个场景。我想搜索一个有时间戳的文件名。我这样做了:C# 搜索具有时间戳的文件,c#,C#,我的应用程序中有这样一个场景。我想搜索一个有时间戳的文件名。我这样做了: if (System.IO.File.Exists("C:\\ScriptLog\\ScriptLog_" + DateTime.Now.ToString("dd_MM_yyyy_HH") + ".log") == true) { //tasks } 但这似乎不起作用…有人能在这方面提供支持吗 我想要的是,我应该能够搜索在那个小时创建的文件。假设当前时间是10:43:04,那么我应该能够检查10:00:00到
if (System.IO.File.Exists("C:\\ScriptLog\\ScriptLog_" + DateTime.Now.ToString("dd_MM_yyyy_HH") + ".log") == true)
{
//tasks
}
但这似乎不起作用…有人能在这方面提供支持吗
我想要的是,我应该能够搜索在那个小时创建的文件。假设当前时间是10:43:04,那么我应该能够检查10:00:00到现在创建的文件。这应该可以:
var files = Directory.GetFiles("C:\\ScriptLog\\", "*.log");
foreach(var file in files)
{
var fileInfo = new FileInfo(file);
if(fileInfo.CreationTime >= /*your date here*/)
{
//This is the file you are searching for
}
}
var files = new DirectoryInfo("C:\\ScriptLog")
.EnumerateFiles("*.log")
.Where(x => x.CreationTime.Hour >= DateTime.Now.Hour);
if (files.Any() != null)
{
// tasks
}
对我来说,你应该得到整个目录的文件列表,然后检查时间戳。 如果只想检查当前小时,则代码如下所示:
string[] ListFiles = System.IO.Directory.GetFiles("C:\\ScriptLog\\", "*.log*");
string currHour = DateTime.Now.ToString("HH");
foreach (string s in ListFiles)
{
if (s.EndsWith(currHour + ".log"))
{
//tasks
}
}
j您能显示示例文件名吗?