using System; using System.Collections.Generic; using System.Text; using System.IO;
namespace filecopy { public class Class1 { static void Main() { Class1 copy = new Class1(); DirectoryInfo sourcedinfo = new DirectoryInfo(@"E:\PFiles"); DirectoryInfo destinfo = new DirectoryInfo(@"E:\dotnet"); copy.CopyAll(sourcedinfo ,destinfo); Console.Read(); } 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()) { Console.WriteLine(@"Copying {0}\{1}", target.FullName, fi.Name); 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); } Console.WriteLine("Success"); } catch (IOException ie) { Console.WriteLine(ie.Message); } } } }
|
| Author: Sasikumar 20 Feb 2009 | Member Level: Gold Points : 1 |
Hi,
The Code you submitted is excellent
It works fine for me
Thanks for the Code
|
| Author: VMRK 01 Oct 2009 | Member Level: Silver Points : 0 |
very useful
|