Register  |  Login




Advertisement

Start Your Own Q&A Site

Create your own Q&A site easily, allowing you to quickly grow a new community around any subject matter or generate new organic traffic for your existing website.

Question

Status: Closed Points: 75 Time: 08:12 - Mar 17, 2009  

dave

How do I copy everything within a folder to another folder using c#.net?

I need to copy all files, folders etc. within a specific folder to another folder.
How do I accomplish this with C# in .net?
I was trying to use the Directory class, but can't see any method for this purpose.

I'm using .net 2.0

Hope you can help...

Categories

Answer Discussion
Tutorials

 

ddrakonn

Date:: Mar 17, 2009

Time:: 11:58

System.IO.File.Copy(sourcepath,destinationpath)
....
System.IO.Directory.Move(sourcepath, destpath)
....
string[] files = System.IO.Directory.GetFiles(path)
foreach(string path in files)
{
System.IO.File.Copy(path,destinationpath)
}

string[] dirs = System.IO.Directory.GetDirectories()
foreach(string dir in dirs)
{
destdir = destinationpath + "\\" + dir;
System.IO.Directory.CreateDirectory(destdir);
}

Somathing like this.

dave

Date:: Mar 17, 2009

Time:: 12:10

Thanks ddrakonn. I though there would be a command to do this without having to loop through all the files and folders manually.
I understand the principle of what you posted, but your code doesn't look completely tested. I mean how does it copy files or folders within folders. I think it lacks recursiveness.
But I did find a method that does what I wanted and looks similar to yours:
http://xneuron.wordpress.com/2007/04/12/...

public static void CopyAll(DirectoryInfo source, DirectoryInfo target)
{
// Check if the target directory exists, if not, create it.
if (Directory.Exists(target.FullName) == false)
{
Directory.CreateDirectory(target.FullName);
}

// Copy each file into it’s 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 each subdirectory using recursion.
foreach (DirectoryInfo diSourceSubDir in source.GetDirectories())
{
DirectoryInfo nextTargetSubDir =
target.CreateSubdirectory(diSourceSubDir.Name);
CopyAll(diSourceSubDir, nextTargetSubDir);
}
}

Question Answered

This question has been closed, and points have been rewarded to the following experts:


ddrakonn: 35
dave: 40

You're welcome however to comment or give additional information or if you wish, you have the ability to write a Tutorial in the Tutorial Area.

Answer this Question

New User

Email:

Upon submission of this form, you will automatically be registered as a Quomon user and we will send your login information to this address

Registered User

Username:

Password:

Forgot Your Password?

No tutorials have been submitted yet. Want to be the first?

Answer this Question

New User

Email:

Upon submission of this form, you will automatically be registered as a Quomon user and we will send your login information to this address

Registered User

Username:

Password:

Forgot Your Password?

Ask a Question

Have a new question? Ask!

You have 100 characters to use



Top Experts

View More

Rank

Expert

Points

1.

nidhi

10279

2.

oracleofDelphi

6493

3.

rcastagna

5596

4.

LAGM

4848

5.

PeterNZ

3487

6.

gonzalo

2840

7.

Mason

2770

8.

jgivoni

2303

9.

xarcus

1820

10.

Anpanman

917

Become an Expert

Register today to share your knowledge with the community and be recognized and rewarded for your contributions.


Register Here




"Psst, Quomon is a great site. Pass it on."     Tell a Friend  |   Link To Us  |   Save to Delicious  |   Digg! Digg it



Language Options

English:

www.quomon.com

Español:

www.quomon.es