Jul 09
Sadly there is no built-in function in System.IO that will copy a folder and its contents. Following is a simple recursive algorithm that copies a folder, its sub-folders and files, creating the destination folder if needed. For simplicity, there is no error handling; an exception will throw if anything goes wrong, such as null or invalid paths or if the destination files already exist.
using System;
using System.IO;
namespace CSharp411
{
class Program
{
static void Main( string[] args )
{
CopyFolder( @"C:\temp\test", @"C:\temp\out" );
Console.ReadLine();
}
static public void CopyFolder( string sourceFolder, string destFolder )
{
if (!Directory.Exists( destFolder ))
Directory.CreateDirectory( destFolder );
string[] files = Directory.GetFiles( sourceFolder );
foreach (string file in files)
{
string name = Path.GetFileName( file );
string dest = Path.Combine( destFolder, name );
File.Copy( file, dest );
}
string[] folders = Directory.GetDirectories( sourceFolder );
foreach (string folder in folders)
{
string name = Path.GetFileName( folder );
string dest = Path.Combine( destFolder, name );
CopyFolder( folder, dest );
}
}
}
}
Related posts:


That helps too much.
Thank you
thanks, it was very helpful, i was wondering if it works as a windows application
Thanks!
great help. very good example explained in simple and easy way thanks
I would push them on to a stack and process the stack in a while loop than call the function recursively. Good example.
Thanks a lot for publishing this code it merely completed my whole project scope
It works perfectly.
Thanks a lot…
It should be easier if Microsoft provided us ways to copy subdirectories… ;D ;D ;D
what I am doing is to use Start method from the Process class from System.Diagnostics namespace
Process.Start(, );
using xcopy.exe inside windows\system32
e.g.: xcopy /E
;D ;D ;D
It should be easier if Microsoft provided us ways to copy subdirectories… ;D ;D ;D
what I am doing is to use Start method from the Process class from System.Diagnostics namespace
Process.Start(-program name-, -arguments-);
using xcopy.exe inside windows\system32
e.g.: xcopy -sourcedirectory- -targetdirectory- /E
;D ;D ;D
Thanks……….
It helped me alot…….
Thanks a lot.
It works very nicely in windows mobile too.
Thanks alot.. It was awesome..
Thanks man, exactly what I needed
thanks, its such a nice things