C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » File Operations »

Copy files from one directory to another


Posted Date: 19 Feb 2009    Resource Type: Code Snippets    Category: File Operations
Author: DharmarajMember Level: Diamond    
Rating: 1 out of 5Points: 10




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);
}
}
}
}



Responses

Author: Sasikumar    20 Feb 2009Member Level: Gold   Points : 1
Hi,

The Code you submitted is excellent

It works fine for me

Thanks for the Code


Author: VMRK    01 Oct 2009Member Level: Silver   Points : 0
very useful


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Copying files from one directory to another  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Comparing Files
Previous Resource: File download
Return to Discussion Resource Index
Post New Resource
Category: File Operations


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use