namespace RunAfterFirstScan
{
class Program
{
public static IEnumerable<string> GetNewest(string path)
{
DateTime to_date = DateTime.Today.AddDays(-5);
var directoryInfo = new DirectoryInfo(path);
if (!directoryInfo.Exists) return Enumerable.Empty<string>();
var query =
from file in directoryInfo.GetFiles()
where file.LastWriteTime.Date == to_date.Date
select file.Name;
return query;
}
static void Main(string[] args)
{
string sourcePath = @"C:\Users\berryn01\Desktop\From\";
string targetPath = @"C:\Users\berryn01\Desktop\To\";
if (!System.IO.Directory.Exists(targetPath))
{
System.IO.Directory.CreateDirectory(targetPath);
}
if (System.IO.Directory.Exists(sourcePath))
{
string[] files = GetNewest(sourcePath).ToArray();
foreach (string s in files)
{
string fileName = System.IO.Path.GetFileName(s);
string destFile = System.IO.Path.Combine(targetPath, fileName);
System.IO.File.Copy(s, destFile);
}
}
else
{
Console.WriteLine("Source path does not exist!");
}
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
}
}
答案 0 :(得分:0)
您的查询错误。我认为根据您的需要,它必须是>=
或<=
。
var query =
from file in directoryInfo.GetFiles()
where file.LastWriteTime.Date <= to_date.Date
select file.Name;
或强>
var query =
from file in directoryInfo.GetFiles()
where file.LastWriteTime.Date >= to_date.Date
select file.Name;