Copy files from Source directory to Destination Directory using C#

No.of Views2108
Bookmarked0 times
Downloads 
Votes0
By  Jana   On  15 Feb 2010 23:02:44
Tag : CSharp , How to
Copy files from Source directory to Destination Directory using C#
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

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

 
Sign Up to vote for this article
 
About Author
 
Jana
Occupation-Not Provided
Company-Not Provided
Member Type-Fresh
Location-Not Provided
Joined date-19 Jun 2009
Home Page-Not Provided
Blog Page-Not Provided
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top