没有现成的.
=========================
using System;
using System.IO;namespace FrameworkExamples
{
//HOW TO: recursively copy all the files and sub dirs in a given directory
// to another directory class SampleRecursiveCopy
{
static void Main()
{
string srcdir, destdir;
bool recursive; recursive = true;
srcdir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), "images");
destdir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), "images2"); FileCopy(srcdir, destdir, recursive);
} private static void FileCopy(string srcdir, string destdir, bool recursive)
{
DirectoryInfo dir;
FileInfo[] files;
DirectoryInfo[] dirs;
string tmppath; //determine if the destination directory exists, if not create it
if (! Directory.Exists(destdir))
{
Directory.CreateDirectory(destdir);
} dir = new DirectoryInfo(srcdir);
//if the source dir doesn't exist, throw
if (! dir.Exists)
{
throw new ArgumentException("source dir doesn't exist -> " + srcdir);
} //get all files in the current dir
files = dir.GetFiles(); //loop through each file
foreach(FileInfo file in files)
{
//create the path to where this file should be in destdir
tmppath=Path.Combine(destdir, file.Name); //copy file to dest dir
file.CopyTo(tmppath, false);
} //cleanup
files = null;
//if not recursive, all work is done
if (! recursive)
{
return;
} //otherwise, get dirs
dirs = dir.GetDirectories(); //loop through each sub directory in the current dir
foreach(DirectoryInfo subdir in dirs)
{
//create the path to the directory in destdir
tmppath = Path.Combine(destdir, subdir.Name); //recursively call this function over and over again
//with each new dir.
FileCopy(subdir.FullName, tmppath, recursive);
}
//cleanup
dirs = null;
dir = null;
}
}
}
=========================
using System;
using System.IO;namespace FrameworkExamples
{
//HOW TO: recursively copy all the files and sub dirs in a given directory
// to another directory class SampleRecursiveCopy
{
static void Main()
{
string srcdir, destdir;
bool recursive; recursive = true;
srcdir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), "images");
destdir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), "images2"); FileCopy(srcdir, destdir, recursive);
} private static void FileCopy(string srcdir, string destdir, bool recursive)
{
DirectoryInfo dir;
FileInfo[] files;
DirectoryInfo[] dirs;
string tmppath; //determine if the destination directory exists, if not create it
if (! Directory.Exists(destdir))
{
Directory.CreateDirectory(destdir);
} dir = new DirectoryInfo(srcdir);
//if the source dir doesn't exist, throw
if (! dir.Exists)
{
throw new ArgumentException("source dir doesn't exist -> " + srcdir);
} //get all files in the current dir
files = dir.GetFiles(); //loop through each file
foreach(FileInfo file in files)
{
//create the path to where this file should be in destdir
tmppath=Path.Combine(destdir, file.Name); //copy file to dest dir
file.CopyTo(tmppath, false);
} //cleanup
files = null;
//if not recursive, all work is done
if (! recursive)
{
return;
} //otherwise, get dirs
dirs = dir.GetDirectories(); //loop through each sub directory in the current dir
foreach(DirectoryInfo subdir in dirs)
{
//create the path to the directory in destdir
tmppath = Path.Combine(destdir, subdir.Name); //recursively call this function over and over again
//with each new dir.
FileCopy(subdir.FullName, tmppath, recursive);
}
//cleanup
dirs = null;
dir = null;
}
}
}
public class ShellFiles{[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto, Pack=1)]public struct SHFILEOPSTRUCT{public IntPtr hwnd;[MarshalAs(UnmanagedType.U4)] public int wFunc;public string pFrom;public string pTo;public short fFlags;[MarshalAs(UnmanagedType.Bool)] public bool fAnyOperationsAborted;public IntPtr hNameMappings;public string lpszProgressTitle;}[DllImport("shell32.dll", CharSet=CharSet.Auto)]static extern int SHFileOperation(ref SHFILEOPSTRUCT FileOp);const int FO_DELETE = 3;const int FO_COPY = 2;const int FOF_ALLOWUNDO = 0x40;const int FOF_NOCONFIRMATION = 0x10; //Don't prompt the user.;const int FOF_SIMPLEPROGRESS = 0x100;public void SendToRecyclyBin(string path){SHFILEOPSTRUCT shf = new SHFILEOPSTRUCT();shf.wFunc = FO_DELETE;shf.fFlags = FOF_ALLOWUNDO | FOF_NOCONFIRMATION;shf.pFrom = path;SHFileOperation(ref shf);}public void Copy(string from, string to){DirectoryInfo source = new DirectoryInfo(from);DirectoryInfo dest = new DirectoryInfo(to);if(!dest.Exists)dest.Create();SHFILEOPSTRUCT shf = new SHFILEOPSTRUCT();shf.wFunc = FO_COPY;shf.fFlags = FOF_ALLOWUNDO;shf.pFrom = from;shf.pTo = to;SHFileOperation(ref shf);}
}
方法二:递归调用(程序简单,但比较慢)
public sealed class DirectoryUtils
{
private DirectoryUtils()
{
}
/// <summary>
/// Copies a directory to a new location.
/// </summary>
/// <param name="src">Source directory path</param>
/// <param name="dest">Destination directory path</param> public static void CopyDirectory(String src, String dest)
{
DirectoryInfo di = new DirectoryInfo(src); foreach(FileSystemInfo fsi in di.GetFileSystemInfos())
{
String destName = Path.Combine(dest, fsi.Name);
if (fsi is FileInfo)
File.Copy(fsi.FullName, destName);
else
{
Directory.CreateDirectory(destName);
CopyDirectory(fsi.FullName, destName);
}
}
}
}