2017-12-01 4 views
1

폴더 및 파일 목록이있는 첨부 된 이미지로 .csv 파일이 있습니다. .csv 파일을 읽고 다른 폴더 아래에서 동일한 폴더 구조를 다시 만들고 싶습니다. 나는 C가 예를 들어File.Move 오류를 사용하면 파일을 만들 수 없습니다.

enter image description here

말을 : \ 데이터 \ SourceFolder \ Folder2와 \ Folder4 \은 File2.txt를, 내가 파일을 원하는 C로 이동합니다 : \ 데이터 \ FilesCopiedfromC \ SourceFolder \ Folder2와 \ Folder4 \ File1.txt. 위의 대상 경로에서 C : \ Data \ FilesCopiedfromC는 항상 같을 것입니다. 대상에 폴더 구조를 만들 수 있지만 source에서 대상으로 file.move를 수행 할 때 "이미 파일이 존재할 때 파일을 만들 수 없습니다."라는 메시지가 나타납니다.

try 
     { 
      string inputfile = textBox1.Text.ToString(); 
      using(StreamReader reader = new StreamReader(inputfile)) 
      { 
       string headerline = reader.ReadLine(); 
       Boolean firstline = true; 
       string line = string.Empty; 
       string SourceFileNameCSV; 
       string SourceFilePathCSV,totalSourceFilePath, strConstructedDestinationfullpath; 
       string[] parts; 
       while ((line = reader.ReadLine()) != null) 
       { 
        char[] delimiters = new char[] { ',' }; 
        parts= line.Split(delimiters); 
        if (parts.Length > 0) 
        { 
         SourceFilePathCSV = parts[0]; 
         SourceFileNameCSV = parts[1]; 
         totalSourceFilePath = SourceFilePathCSV + "\\" + SourceFileNameCSV; 
         strDestinationDynamicPath = SourceFilePathCSV.Replace("C:\\Data\\", " ").TrimEnd(); 
        strConstructedDestinationfullpath = Path.Combine(strDestinationStaticPath, strDestinationDynamicPath); 
         if (!string.IsNullOrEmpty(strConstructedDestinationfullpath)) 
         { 
          if (!Directory.Exists(strDestinationDynamicPath)) 
          { 
           Directory.CreateDirectory(strConstructedDestinationfullpath); 
          } 
          // File.Move(totalSourceFilePath, strConstructedDestinationfullpath); 
         } 
        } 

       } 
      } 

     }//try 

도움을 주시면 감사하겠습니다.

+0

파일이 이미 존재하는지 확인하거나 copy() 메소드를 사용하는 경우 true 또는 false를 사용하는 매개 변수를 확인해야합니다. 'true로 설정하면 이미 존재하는 파일을 덮어 씁니다. file move는 src 파일, dest 파일을 기대합니다. 파일이 이미'쉼표로 구분 된 경우에도 조건부 검사를 작성해야합니다. 그러면 대상에서 파일을 변경하십시오 ext .csv 파일을 분할 할 필요가 없습니다 – MethodMan

답변

1

그것입니다. 그렇다면 파일이 삭제되었는지 확인하십시오 :

if (System.IO.File.Exists("filename")) 

{ 

//delete 

    System.IO.File.Delete("filename"); //try/catch exception handling 
    needs to be implemented 

} 
+0

감사합니다 Emilio Ceroleni, 나는 조건을 추가하고 알려드립니다 – gvii

2

는 현재 그냥 경로를 제공하고, 대상의 파일 이름을 지정해야합니다 : 분명히, 파일이 이미 대상에 존재하기 때문에

File.Move(
    totalSourceFilePath, 
    Path.Combine(strConstructedDestinationfullpath, Path.GetFileName(totalSourceFilePath)); 
+0

Alex K. IT 덕분에 작동하는 것 같습니다. 나는 더 시험 할 것이고 당신에게 돌아갈 것이다 – gvii

+0

그것은 일하고있다. 감사합니다 – gvii

관련 문제