0

我正在尝试查找具有最后写入日期的文件并将其复制到其他位置。它正确找到了文件,但是当我尝试复制它时,它找不到刚刚找到的文件。这是在 SSIS 脚本任务中。

DirectoryInfo directory = new DirectoryInfo(@"path");
FileInfo[] files = directory.GetFiles();

//files that have been written to in the last 3 days
DateTime lastWrite = DateTime.Now.AddDays(-3); 

foreach (FileInfo latestFile in files)
{  
    // if its the correct name
    if (latestFile.Name.StartsWith("OMC")) 
    {
        // if its in the last 3 days
        if (latestFile.LastWriteTime > lastWrite) 
        {    
            lastWrite = latestFile.LastWriteTime;

            // this correctly find the file and puts it into the file variable.
            file = latestFile.ToString(); 

            // this errors out saying it cannot find the file.
            // (Does not even go to the outputFile)
            File.Copy(file, outputFile, true); // <- error

            //backs the file up 
            File.Copy(file, backupfile, true);
        }
    }   
}
4

3 回答 3

4

FileInfo.ToString()返回文件的名称,但为了复制它,您需要完整路径。改变

file = latestFile.ToString();

file = latestFile.FullName;

试一试。

于 2013-09-04T21:08:51.773 回答
2

latestFile.ToString()评估什么?那是一种奇怪的获取路径的方法。

FileInfo.FullName像文档指示的那样使用。

您可以使用调试器自己查找此类错误。

于 2013-09-04T21:08:48.050 回答
2

您可能需要构建完整路径而不是使用Fileinfo.ToString()

file = latestFile.FullName; 

来自MSDN

在某些情况下,ToString 方法返回的字符串不代表完全限定的路径。例如,当您使用 GetFiles 方法创建 FileInfo 对象时,ToString 方法并不代表完全限定的路径。

于 2013-09-04T21:08:59.593 回答