2013-08-02 5 views
2

소스를 목표로하고 대상은 다음과 같이 같은 서브 디렉토리가 있습니다C# 복사 파일을 폴더를

C : \ FS \ 소스 \ \
C : \ FS \ 소스 \ B 형 \

C : \ FS \ 대상 \ \
C : \ FS \ 대상 \ B \

내가 파일을 존재하지 않는 경우 대상 소스에서 파일을 복사와 싸우고 있어요. C#에서 대상 폴더와 원본 폴더를 비교하는 가장 좋은 방법은 무엇입니까 - 대상 파일이 종료되지 않았는지 확인하고 특정 소스 (c : \ fs \ source \ a \ config.xml 및 app.config)의 파일을 특정 대상으로 복사하십시오 (c : \ fs \ target \ a \). 대상 파일이 있으면 무시하십시오. C#으로 작성하는 방법?

코드 예제가 매우 감사합니다. 감사! 루프 내부에이를 넣어

string curFile = @"c:\temp\test.txt"; 
Console.WriteLine(File.Exists(curFile) ? "File exists." : "File does not exist."); 

: 그것은이 방법으로 존재하는 경우

public void TargetFileCreate() 
    { 
     foreach (var folder in SourceFolders) 
     { 
      string[] _sourceFileEntries = Directory.GetFiles(folder); 

      foreach (var fileName in _sourceFileEntries) 
      { //dont know how to implement here: 
       //how to compare source file to target file to check if files exist or not 
       //c:\fs\source\A\config.xml compares to c:\fs\target\A\ (no files) that should be pasted 
       //c:\fs\source\B\config.xml compares to c:\fs\target\B\config.xml that is already existed - no paste 
      } 
     } 
    } 
+0

나는이 폴더가 가지고 있겠지 경우 특정 폴더를 비교하고 확인할 수있는 소스 폴더와 대상 폴더간에 두 루프 (foreach 문)에서 검색하는 것을 시도 파일, 소스에서이 폴더로 파일을 복사하십시오. 내 코드가 매우 이상하게 보입니다. 나는 그것을 코딩하는 더 좋은 방법을보고 싶다 .... – user235973457

답변

0

각 파일을 확인할 수 있습니다. 거기서 그 파일들을 복사하십시오.

MSDN 코드는 :

// Simple synchronous file copy operations with no user interface. 
// To run this sample, first create the following directories and files: 
// C:\Users\Public\TestFolder 
// C:\Users\Public\TestFolder\test.txt 
// C:\Users\Public\TestFolder\SubDir\test.txt 
public class SimpleFileCopy 
{ 
    static void Main() 
    { 
     string fileName = "test.txt"; 
     string sourcePath = @"C:\Users\Public\TestFolder"; 
     string targetPath = @"C:\Users\Public\TestFolder\SubDir"; 

     // Use Path class to manipulate file and directory paths. 
     string sourceFile = System.IO.Path.Combine(sourcePath, fileName); 
     string destFile = System.IO.Path.Combine(targetPath, fileName); 

     // To copy a folder's contents to a new location: 
     // Create a new target folder, if necessary. 
     if (!System.IO.Directory.Exists(targetPath)) 
     { 
      System.IO.Directory.CreateDirectory(targetPath); 
     } 

     // To copy a file to another location and 
     // overwrite the destination file if it already exists. 
     System.IO.File.Copy(sourceFile, destFile, true); 

     // To copy all the files in one directory to another directory. 
     // Get the files in the source folder. (To recursively iterate through 
     // all subfolders under the current directory, see 
     // "How to: Iterate Through a Directory Tree.") 
     // Note: Check for target path was performed previously 
     //  in this code example. 
     if (System.IO.Directory.Exists(sourcePath)) 
     { 
      string[] files = System.IO.Directory.GetFiles(sourcePath); 

      // Copy the files and overwrite destination files if they already exist. 
      foreach (string s in files) 
      { 
       // Use static Path methods to extract only the file name from the path. 
       fileName = System.IO.Path.GetFileName(s); 
       destFile = System.IO.Path.Combine(targetPath, fileName); 
       System.IO.File.Copy(s, destFile, true); 
      } 
     } 
     else 
     { 
      Console.WriteLine("Source path does not exist!"); 
     } 

     // Keep console window open in debug mode. 
     Console.WriteLine("Press any key to exit."); 
     Console.ReadKey(); 
    } 
} 
2
foreach (var file in Directory.GetFiles(source)) 
{ 
    File.Copy(file, Path.Combine(target, Path.GetFileName(file)), false); 
} 
0
foreach (var file in Directory.GetFiles(source)) 
{ 
    var targetFile = System.IO.Path.Combine(target, System.IO.Path.GetFileName(file)); 
    if(!File.Exists(targetFile)) 
    { 
     File.Copy(file, targetFile) 
    } 
} 
관련 문제