using System;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
static void Main( string[] args )
{
// Copies the content of the source directory to a target
// directory.
// You can change the implementation to actually copy the
// whole SourceDir (including SourceDir itself), but
// here only the content is copied.
//
// Also, you will probably want some more checking
// to make copy safe.
// Do you want to overwrite files?
// Do you want to allow a copy from source to source?
// ...
CopyDir( @"C:\Temp\SourceDir", @"C:\Temp\TargetDir" );
Console.ReadLine();
}
public static void CopyDir( string source, string target )
{
Console.WriteLine(
"\nCopying Directory:\n \"{0}\"\n-> \"{1}\"",
source, target );
if ( !Directory.Exists( target ) )
Directory.CreateDirectory( target );
string[] sysEntries =
Directory.GetFileSystemEntries( source );
foreach ( string sysEntry in sysEntries )
{
string fileName = Path.GetFileName( sysEntry );
string targetPath = Path.Combine( target, fileName );
if ( Directory.Exists( sysEntry ) )
CopyDir( sysEntry, targetPath );
else
{
Console.WriteLine( "\tCopying \"{0}\"", fileName );
File.Copy( sysEntry, targetPath, true );
}
}
}
}
}