2016-08-26 7 views
0

ソースフォルダABCのサブフォルダをコピーしてコピー先のフォルダに貼りたいと思います。しかし、それは動作していません。ここでは私のC#のコードですが、それは正常ですが、サブフォルダだけではなくフォルダ全体をコピーします。asp.netの別のディレクトリにファイルを持つサブフォルダを移動するにはC#

// string fileName = "test.txt"; 
string sourcePath = "D:\\Shraddha\\Demo_Web_App\\Source"; 
string targetPath = "D:\\Shraddha\\Demo_Web_App\\Destination"; 

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

// 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); 
     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(); 
+0

のディレクトリ構造を維持するためにだから、ファイルはXXの\ソース\で直接あなただけのXX \ソースの下にフォルダとそのコンテンツファイルをコピーしたい、あなたが意味するか\ではなく?そして、それはフォルダ全体をコピーすると、あなたはフォルダのソースのすべての内容を意味するのですか、それともあなたの目的地フォルダの下に新しいフォルダ "ソース"を作成しますか? –

+0

はいサーあなたのコードを進めてください –

答えて

2

さてさて、ここで私達は行く:

これは本当に理にかなっていません。 targetPathが存在する場合は、targetPathフォルダを作成しますか?

if (System.IO.Directory.Exists(targetPath)) 
{ 
    System.IO.Directory.CreateDirectory(targetPath); 
} 

はおそらく意味:

if (!System.IO.Directory.Exists(targetPath)) 
{ 
    System.IO.Directory.CreateDirectory(targetPath); 
} 

あなたが開始するためにすべてのディレクトリを取得し、最初に行うために必要なもの:あなたはforeachのとallDirectoriesループスルー、見つけることができ、その後

var allDirectories = Directory.GetDirectories(targetPath, "*", SearchOption.AllDirectories); 

各フォルダのすべてのファイルをコピーして内容をコピーします。

+0

親愛なる私のニーズを満たす完全なコードを記述するのに役立ちます。 –

+2

@SantoshKumarBind StackOverflowは無料のコーディングサービスではなく、コラボレーションサイトです。私はすでにすべてのサブディレクトリを見つけるためのコードを提供しており、これらをループすることができます。あなたは実際にそれをする方法を学ぶために、その周りを遊ばなければなりません。私たちが書いたコードをコピー/貼り付けするのではなく、 – uTeisT

0

次の行が提供するように動作することはできません。

destFile = System.IO.Path.Combine(targetPath); 

File.Copyは、あなたが「」からコンテンツをコピーしたいファイルへのパスを期待していますが、のみ提供されています移動先フォルダ。 Path.Combineメソッドにファイル名を含める必要があります。

たとえば、Path.GetFileNameメソッドでパス文字列を解析する場合、Path.Combineへの追加引数として結果(完全なソースパスのないファイル名のみ)を渡して、有効な宛先パスを生成できます。

さらに、コード例ではルートソースフォルダの直下に置かれているファイルしかコピーしていないので、uteistは既に言いましたように、すべてのサブディレクトリを最初に取得する必要があります。

0

foreach (var dir in System.IO.Directory.GetDirectories(sourcePath)) 
      { 
       var dirInfo = new System.IO.DirectoryInfo(dir); 
       System.IO.Directory.CreateDirectory(System.IO.Path.Combine(targetPath, dirInfo.Name)); 
       foreach (var file in System.IO.Directory.GetFiles(dir)) 
       { 
        var fileInfo = new System.IO.FileInfo(file); 
        fileInfo.CopyTo(System.IO.Path.Combine(targetPath, dirInfo.Name, fileInfo.Name)); 
       } 
      }; 
関連する問題