C# 按LastWriteTime复制文件

C# 按LastWriteTime复制文件,c#,C#,我撞到砖墙了!我的目标是仅在给定的一天在两个文件夹之间复制文件,而不考虑与较早的LastWriteTime位于同一文件夹中的文件。我的逻辑似乎正常,但是我在运行代码时收到一个异常,说明它找不到我的测试文件,即使它不能告诉我测试文件的名称!!:(我已经在下面发布了我的代码,请帮忙 namespace RunAfterFirstScan { class Program { public static IEnumerable<string> GetNewest(s

我撞到砖墙了!我的目标是仅在给定的一天在两个文件夹之间复制文件,而不考虑与较早的LastWriteTime位于同一文件夹中的文件。我的逻辑似乎正常,但是我在运行代码时收到一个异常,说明它找不到我的测试文件,即使它不能告诉我测试文件的名称!!:(我已经在下面发布了我的代码,请帮忙

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();

    }
}
}
namespace RunAfterFirstScan
{
班级计划
{
公共静态IEnumerable getlatest(字符串路径)
{
DateTime to_date=DateTime.Today.AddDays(-5);
var directoryInfo=新的directoryInfo(路径);
如果(!directoryInfo.Exists)返回Enumerable.Empty();
变量查询=
来自directoryInfo.GetFiles()中的文件
其中file.LastWriteTime.Date==to_Date.Date
选择文件名;
返回查询;
}
静态void Main(字符串[]参数)
{
字符串sourcePath=@“C:\Users\berryn01\Desktop\From\”;
字符串targetPath=@“C:\Users\berryn01\Desktop\To\”;
如果(!System.IO.Directory.Exists(targetPath))
{
System.IO.Directory.CreateDirectory(targetPath);
}
if(System.IO.Directory.Exists(sourcePath))
{
string[]files=GetNewest(sourcePath).ToArray();
foreach(文件中的字符串s)
{
字符串文件名=System.IO.Path.GetFileName(s);
字符串destFile=System.IO.Path.Combine(targetPath,文件名);
System.IO.File.Copy(s,destFile);
}
}
其他的
{
WriteLine(“源路径不存在!”);
}
控制台。WriteLine(“按任意键退出”);
Console.ReadKey();
}
}
}

您的查询是错误的。我想一定是
=
哪个部分不起作用。是否有任何异常或错误?请详细说明您的问题。如果您的文件是今天创建的,它将永远找不到,因为您在GetNew方法中减去了5天。此外,我认为比较
file.LastWriteTime.Date==to_Date.Date
是一个错误,应该是
file.LastWriteTime.Date>=to_Date.Date
抱歉,详细说明一下,有一个异常:mscorlib.dll中发生了一个类型为“System.IO.FileNotFoundException”的未经处理的异常。其他信息:找不到文件“Opinion Set Routine.docx”。我不太担心日期,因为我刚刚使用了g使用减法测试先前创建的文件夹中的文件。但是,每次我更改此值时,它都会在给定日期找到正确的文件,然后抛出一个异常,表示找不到该文件。谢谢你们,我现在将尝试此操作。好的,我已将查询更改为“>=”,但收到相同的异常。假设我今天在文件夹中创建了文件呃,用“DateTime to_date=DateTime.Today”运行它,我会被抛出异常,但是如果我返回一天,它没有发现任何异常,那么代码就会执行得很好。在我看来,它正在正确地查找文件,然后在运行复制方法时不知何故没有使用完整路径?我不确定,我对此感到困惑。顺便说一句,很抱歉我的格式设置,只是le在这里学习道路规则。当我调用
System.IO.File.Copy(s,destFile)时,我能想到的最好方法是;
它不再知道源路径,只知道文件名。你知道我该如何重新格式化吗?问题解决了!!!原来我需要将路径与文件名重新组合,因为我的getnewst方法只剥离了文件名。我编辑了我的foreach,以包括:
string srcFile=System.IO.path.Combine(sourcePath,s);string destFile=System.IO.Path.Combine(targetPath,s);System.IO.File.Copy(srcFile,destFile);
还有,谢谢你的帮助!我希望这会对将来的人有所帮助。
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;