In this code snippets I have described how to copy the entire directory that contain files, sub directory to another directory We need to add the following Namespace For doing the IO Operation using System.IO; Below is the CopyDirectory Method which will call recoursively to copy the content from source to destination {codecitation class="brush: c#; gutter: true;" width="500px"} /// /// Copy Content of Source Directory To Destination Directory /// /// Source Path /// Destination Path public void CopyDirectory(string strSource,string strDestination) { string strDestinationFile = string.Empty; //Get The Directory Information
DirectoryInfo DirInfo=new DirectoryInfo(strSource);
//Check if Destination Exist elase Create It if(!Directory.Exists(strDestination)) { Directory.CreateDirectory(strDestination); }
//Start The File Copying Operation foreach (FileSystemInfo filesInfo in DirInfo.GetFileSystemInfos()) { strDestinationFile = Path.Combine(strDestination, filesInfo.Name);
//Check if its a file if (filesInfo is FileInfo) { File.Copy(filesInfo.FullName, strDestinationFile, true); } else { //Recursive Call to the same function CopyDirectory(filesInfo.FullName, strDestinationFile); }
} } {/codecitation} How to invoke the method ? CopyDirectory("f:\\wallpaper", "f:\\New wallpapers"); Thank you Abhijit |