C#とASP.NETを使用して、フォルダのコンテンツを別のフォルダに再帰的にコピーする最も良い方法は何ですか?C#でコンテンツを再帰的にコピーする最良の方法は何ですか?
答えて
さてあなたはこの
DirectoryInfo sourcedinfo = new DirectoryInfo(@"E:\source");
DirectoryInfo destinfo = new DirectoryInfo(@"E:\destination");
copy.CopyAll(sourcedinfo, destinfo);
を試すことができ、これはすべての作業を行う方法であって、
public void CopyAll(DirectoryInfo source, DirectoryInfo target)
{
try
{
//check if the target directory exists
if (Directory.Exists(target.FullName) == false)
{
Directory.CreateDirectory(target.FullName);
}
//copy all the files into the new directory
foreach (FileInfo fi in source.GetFiles())
{
fi.CopyTo(Path.Combine(target.ToString(), fi.Name), true);
}
//copy all the sub directories using recursion
foreach (DirectoryInfo diSourceDir in source.GetDirectories())
{
DirectoryInfo nextTargetDir = target.CreateSubdirectory(diSourceDir.Name);
CopyAll(diSourceDir, nextTargetDir);
}
//success here
}
catch (IOException ie)
{
//handle it here
}
}
私はこれが:)
ちょうどMicrosoft.VisualBasic.dll
でMicrosoft.VisualBasic.FileIO.FileSystem.CopyDirectory
を使用するのに役立つことを願っていますアセンブリ。
あなたは再帰的にフォルダをダウン検索するSearchOption.AllDirectories
を使用することができますMicrosoft.VisualBasic
Microsoft.VisualBasic.FileIO.FileSystem.CopyDirectory(source, destination);
音が便利です。どのようにディレクトリクラスにはこのメソッドがありませんでしたか? –
への参照を追加し、あなたはちょうどあなたがコピーする前に、ディレクトリを作成する必要があります...
// string source, destination; - folder paths
int pathLen = source.Length + 1;
foreach (string dirPath in Directory.GetDirectories(source, "*", SearchOption.AllDirectories))
{
string subPath = dirPath.Substring(pathLen);
string newpath = Path.Combine(destination, subPath);
Directory.CreateDirectory(newpath);
}
foreach (string filePath in Directory.GetFiles(source, "*.*", SearchOption.AllDirectories))
{
string subPath = filePath.Substring(pathLen);
string newpath = Path.Combine(destination, subPath);
File.Copy(filePath, newpath);
}
素晴らしい!私がしなければならなかった唯一の調整は、int pathLen = source.Length + 1 – nabeelfarid
です。乾杯、微調整しました:-) – Keith
- 1. このコードを再帰的にする最良の方法
- 2. データベースをコピーする最良の方法は何ですか?
- 3. Scalaでこの再帰的メソッドの尾を再帰的にする方法は?
- 4. リンクリストを再帰的にコピー
- 5. C/C++でテキストファイルを暗号化する最良の方法は何ですか?
- 6. 「人気コンテンツ」ディスプレイを実装する最良の方法は何ですか?
- 7. Rubyでハッシュを再マップする最良の方法は何ですか?
- 8. xibファイルをコピーする最も良い方法は何ですか?
- 9. 静的コンテンツとAJAXページコンテンツの両方を配信する最良の方法は何ですか?
- 10. xcodeプロジェクトファイルを再作成する最良の方法は何ですか?
- 11. サウンドをファイルにする最良の方法は何ですか?
- 12. C#でモナドを実装する最良の方法は何ですか
- 13. ErlangでC配列を表現する最良の方法は何ですか?
- 14. C++でHTML出力を生成する最良の方法は何ですか?
- 15. 目的Cのintの配列をコピーする最も効率的な方法は何ですか?
- 16. Obj-Cのさまざまなクラスでコードを再利用する最良の方法は何ですか?
- 17. クライアントに強制的にウェブページを再読み込みさせる最良の方法は何ですか?
- 18. C++での再帰的フォルダスキャン
- 19. C++からnetlinkを使用する最良の方法は何ですか?
- 20. C#でRichTextFileを保存する最も良い方法は何ですか?
- 21. C#で.sysファイルをロードする最も良い方法は何ですか?
- 22. C++でデータを追加する最も良い方法は何ですか
- 23. WordPressでC#をフォーマットする最も良い方法は何ですか?
- 24. C#でレーダーをシミュレートする最良の方法は何でしょうか?
- 25. C#.NETでゲームを作る最良の方法は何ですか?
- 26. PHP再帰的配列コピー
- 27. Inno Setup:コードセクションで再帰的にフォルダ、サブフォルダ、ファイルをコピーする
- 28. コンテンツが他のサイトにコピーされないようにする良い方法は何ですか
- 29. 二重リンクリストを再帰的に複製する方法C++
- 30. C#で2つのリストを返す最も良い方法は何ですか?
グレートコード1があります変更するもの: //対象のディレクトリが存在するかどうかを確認する if(Directory.Exists(target.FullName)== false) { ディレクトリ。作成ディレクトリ(targ et.FullName); } あなたは既にあなたが持っているDirectoryInfoオブジェクトだけでよい: if(!target.Exists) { target.Create(); } – greektreat
変更ありませんでした。 –
私は 'target.Exists'が' Directory.Exists(target.FullName) 'がどこで正しく動作しなかったかをちょっと調べました... –