/// <summary>
/// 复制文件夹(及文件夹下所有子文件夹和文件)
/// </summary>
/// <param name="sourcePath">待移动的文件夹路径</param>
/// <param name="destinationPath">目标路径</param>
public static void CopyDirectory(String sourcePath, String destinationPath)
{
var info = new DirectoryInfo(sourcePath);
Directory.CreateDirectory(destinationPath);
foreach (var fsi in info.GetFileSystemInfos())
{
var destName = Path.Combine(destinationPath, fsi.Name);
//如果是文件,移动文件 Ps : 如果复制 使用Copy
if (fsi is FileInfo)
File.Move(fsi.FullName, destName);
//如果是文件夹,新建文件夹,递归
else
{
Directory.CreateDirectory(destName);
CopyDirectory(fsi.FullName, destName);
}
}
//删除旧文件夹
DeleteFolder(sourcePath);
}
/// <summary>
/// 删除文件夹(及文件夹下所有子文件夹和文件)
/// </summary>
/// <param name="deleteDirectory">删除文件夹的路径</param>
public static void DeleteFolder(string deleteDirectory)
{
if (!Directory.Exists(deleteDirectory)) return;
foreach (var deleteFile in Directory.GetFileSystemEntries(deleteDirectory))
{
if (File.Exists(deleteFile))
File.Delete(deleteFile);
else
DeleteFolder(deleteFile);
}
Directory.Delete(deleteDirectory);
}
附带一个微软官方的Demo :
下面的示例演示如何复制文件和目录。
// Simple synchronous file copy operations with no user interface.
// To run this sample, first create the following directories and files:
// C:\Users\Public\TestFolder
// C:\Users\Public\TestFolder\test.txt
// C:\Users\Public\TestFolder\SubDir\test.txt
public class SimpleFileCopy
{
static void Main()
{
string fileName = "test.txt";
string sourcePath = @"C:\Users\Public\TestFolder";
string targetPath = @"C:\Users\Public\TestFolder\SubDir";
// Use Path class to manipulate file and directory paths.
string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
string destFile = System.IO.Path.Combine(targetPath, fileName);
// To copy a folder's contents to a new location:
// Create a new target folder, if necessary.
if (!System.IO.Directory.Exists(targetPath))
{
System.IO.Directory.CreateDirectory(targetPath);
}
// To copy a file to another location and
// overwrite the destination file if it already exists.
System.IO.File.Copy(sourceFile, destFile, true);
// To copy all the files in one directory to another directory.
// Get the files in the source folder. (To recursively iterate through
// all subfolders under the current directory, see
// "How to: Iterate Through a Directory Tree.")
// Note: Check for target path was performed previously
// in this code example.
if (System.IO.Directory.Exists(sourcePath))
{
string[] files = System.IO.Directory.GetFiles(sourcePath);
// Copy the files and overwrite destination files if they already exist.
foreach (string s in files)
{
// Use static Path methods to extract only the file name from the path.
fileName = System.IO.Path.GetFileName(s);
destFile = System.IO.Path.Combine(targetPath, fileName);
System.IO.File.Copy(s, destFile, true);
}
}
else
{
Console.WriteLine("Source path does not exist!");
}
// Keep console window open in debug mode.
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
}
更多微软Demo 请参考:http://msdn.microsoft.com/zh-cn/library/cc148994.aspx