2013-09-04 9 views
0

마지막으로 작성한 날짜의 파일을 찾아 다른 위치로 복사하려고합니다. 파일을 올바르게 찾았지만 복사하려고하면 방금 찾은 파일을 찾을 수 없습니다. 이것은 SSIS 스크립트 작업에 있습니다.FileInfo가 파일을 찾았지만 File.Copy가 파일을 찾을 수 없습니다.

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); 
     } 
    } 
} 
+2

중단 점을 설정할 때'file'에 파일의 전체 경로가 있습니까? – PoweredByOrange

+0

그건 그가 물었던 것이 아닙니다. –

+0

그냥 toString의 fullname을 사용하고 모든 것을 투표하지 마십시오! –

답변

3

FileInfo.ToString()은 파일 이름을 반환하지만 파일을 복사하려면 전체 경로가 필요합니다.

file = latestFile.ToString(); 

file = latestFile.FullName; 

로 변경하고 그것에게 기회를 제공합니다.

+0

감사! 그게 효과가있어! – Rainhider

2

latestFile.ToString()은 (으)로 평가 되나요? 경로를 얻는 이상한 방법입니다.

문서와 마찬가지로 FileInfo.FullName을 사용하십시오.

디버거를 사용하여 버그를 직접 찾을 수 있습니다.

+2

Downvotes는 프롬프트없이 설명되어야합니다. – usr

+0

해설 이상의 아무것도 없습니다. – I4V

+4

당신이 왜 그렇게 생각하는지 잘 모르겠습니다. 문제를 지적하고 해결책을 제시합니다. 그것에 대해 뭐라고합니까? – usr

2

대신 Fileinfo.ToString()를 사용하는 전체 경로를 구성해야 할 수도 있습니다

file = latestFile.FullName; 

MSDN에서을 :

ToString 메서드에 의해 반환되는 문자열 정규화를 나타내지 않는 경우가 있습니다 통로. 예를 들어, GetFiles 메서드를 사용하여 FileInfo 개체를 만들면 ToString 메서드는 정규화 된 경로를 나타내지 않습니다.