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 );
}
}
}
}
Popularity: 33% [?]
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
What about
if a file already exist at destination folder ???
-Thanks,
Best Regards
Thanks, a lot,
Good Stuf
Thanks You Sir superb 100 % working code..
Thanks for the pretty simple and nice example!
What if your copying a directory to its sub-directory?
This algorithm works fine if there is no parent-child relation between the source and destination directory.
If you pass a subdirectory as destination directory this algorithm doesn't take care of it. It will endlessly enumerate the newly created sub folders and makes further copies of them.
If you pass a parent directory of source as destination the following happens: If the directory structure has any element which on the new destination path collides with the root of the source structure there will be undesired happenings: Overwrites or unsuccessful copies because of colliding filenames or duplicated copies.
I'll implement such procedure which get over these problems so this function basically right but have to be a little extended for my needs.
I wrote this using Linq (I don't like recursive functions):
public static void CopyAllFiles(string sourceFolder, string destinationFolder)
{
var FileList = from files in Directory.GetFiles(sourceFolder, "*.*", SearchOption.AllDirectories)
select files;
foreach (string file in FileList)
{
string RelativeFilePath = file.Replace(sourceFolder, "");
string DestinationFilePath = String.Format("{0}{1}{2}",
destinationFolder, Path.DirectorySeparatorChar, RelativeFilePath);
if (!Directory.Exists(Path.GetDirectoryName(DestinationFilePath)))
{
Directory.CreateDirectory(Path.GetDirectoryName(DestinationFilePath));
}
File.Copy(file, DestinationFilePath, true);
}
}
This creates only readonly files and directories even if the destination directory is read-write,
how can I change to read-write all the permissions on this copied files and directories?
this creates a new readonly directory and files,
what if I want them to be read-write?
Thx
Great very helpful
to Enrique: you can remove read-only flag on some files or directories by using this
staticvoid setAttributesNormal(DirectoryInfo dir)
{
// Remove flags from the current directory
dir.Attributes = FileAttributes.Normal;
// Remove flags from all files in the current directory
foreach (FileInfo file in dir.GetFiles())
{
file.Attributes = FileAttributes.Normal;
}
// Do the same for all subdirectories
foreach (DirectoryInfo subDir in dir.GetDirectories())
{
setAttributesNormal(subDir);
}
}
ref : http://msdn.microsoft.com/en-us/library/fxeahc5f.aspx
@Eitan… great solution, thanks! I noticed that it will not copy directories that are empty. Any way to get around that?
You have two options, one is to use Directory.GetFileSystemEntries(,), and copy or create directory according to the entry type (this function returns files and folders as well).
The other option is to do first a loop for folders only by using Directory.GetDirectories(), and create the target folder if it does not exist, if you use this approach, you can remove the folder creation from the file loop.
Many thanks….you saved my day
you can check this post:
http://namita2021.wordpress.com/2010/05/07/system-io-directory-2/
Works great !!!