2016-04-08 21 views
0

この構造に従います。ディレクトリが空であっても、ディレクトリをソースから宛先にコピーする方法はありますか?

A 
A1 
    A111 
A2 
A3 
A4 

B 

私の送信元アドレスは次のとおりです。E:\ A \ A2

私の宛先アドレスはEです:\のB

A2が空になったところ私はBにA2をコピーします。

私はこのコードコード

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

    } 

を使用している場合は、私はC#で、この空のフォルダをコピーすることができますどのようにMSDN からである

BLOCKQUOTE

+0

空であればA2をコピーするのは何ですか? – Kypaz

+1

私は彼がdirが空であってもパスを構築したいと思うと思います。 –

+0

コードはすでにそれを処理しています。何が問題なのですか?どのように調査しましたか? – Luaan

答えて

1

あなたは可能性がありますが、機能DirectoryCopy( ) 例を使用してDirectoryCopy(@ "E:\ A \ A2"、@ "E:\ B"、true)を実行すると、E:\ B \ A2が作成されます。

関連する問題