2016-04-08 3 views
0

Folgen Sie dieser Struktur.Wie kann ich ein Verzeichnis von der Quelle zum Ziel kopieren, auch wenn das Verzeichnis leer ist?

A 
A1 
    A111 
A2 
A3 
A4 

B 

Meine Quelle Adresse lautet: E: \ A \ A2

My Destination Adresse ist E: \ B

Ich möchte A2 in B kopieren, wo A2 leer ist.

Wenn ich-Code verwenden

public void DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs) 
    { 
     // Get the subdirectories for the specified directory. 
     DirectoryInfo dir = new DirectoryInfo(sourceDirName); 
     string vSourceDirName = dir.Name; 
     if (!dir.Exists) 
     { 
      throw new DirectoryNotFoundException(
       "Source directory does not exist or could not be found: " 
       + sourceDirName); 
     } 

     DirectoryInfo[] dirs = dir.GetDirectories(); 
     // If the destination directory doesn't exist, create it. 
     if (!Directory.Exists(destDirName)) 
     { 
      Directory.CreateDirectory(destDirName); 
     } 

     // Get the files in the directory and copy them to the new location. 
     FileInfo[] files = dir.GetFiles(); 
     foreach (FileInfo file in files) 
     { 
      string temppath = Path.Combine(destDirName, file.Name); 
      file.CopyTo(temppath, false); 
     } 

     // If copying subdirectories, copy them and their contents to new location. 
     if (copySubDirs) 
     { 
      foreach (DirectoryInfo subdir in dirs) 
      { 
       string temppath = Path.Combine(destDirName,vSourceDirName,subdir.Name); 
       DirectoryCopy(subdir.FullName, temppath, copySubDirs); 
      } 
     } 

    } 

Dieser Kodex von Msdn ist Wie kann ich diesen Ordner leeren in C# Kopieren

Blockquote

+0

Was ist der Sinn A2 zu kopieren, wenn es leer ist? – Kypaz

+1

Ich denke, er will den Pfad auch dann erstellen, wenn dir leer ist –

+0

Der Code bereits behandelt. Was ist das Problem und wie haben Sie es untersucht? – Luaan

Antwort

1

Sie könnten Sie die Funktion DirectoryCopy () Mit Ihrem Beispiel versuchen DirectoryCopy (@ "E: \ A \ A2", @ "E: \ B", wahr) und Sie erhalten E: \ B \ A2 erstellt

Verwandte Themen