我需要验证文件是否在2小时到6小时之间创建,为此,我使用此解决方案:
var files = new DirectoryInfo(@"X:\").GetFiles("*.*");
foreach(var i in files)
{
var time = i.CreationTime.ToString("HH:mm").Split(':');
var hora = Convert.ToInt32(time[0]);
var minu = Convert.ToInt32(time[1]);
if ((hora >= 2 && hora <= 6) && (minu <= 59))
Console.WriteLine("File created before 6:30 in the morning\n\t{0} -> {1}", i.Name, i.CreationTime.ToString());
}
但是,我认为这个解决方案非常糟糕,我想知道是否有更好的方法来进行验证......
答案 0 :(得分:3)
CreationTime
是你的关键。您需要做的就是:
if ((i.CreationTime.Hour >= 2 && i.CreationTime.Hour <= 6)
&& (i.CreationTime.Minute <= 59))
{
...
}
答案 1 :(得分:0)
很高兴只陈述一次情况:
TimeSpan okFrom = new TimeSpan(2, 0, 0),
okTo = new TimeSpan(6, 0, 0);
var files = new DirectoryInfo(@"X:\").GetFiles("*.*");
foreach (var i in files)
{
if (i.CreationTime.TimeOfDay >= okFrom && i.CreationTime.TimeOfDay <= okTo)
Console.WriteLine("File created before 6:30 in the morning\n\t{0} -> {1}", i.Name, i.CreationTime.ToString());
}