private void CopyDirectory(string sourcePath, string destPath, bool overwrite) { DirectoryInfo sourceDir = new DirectoryInfo(sourcePath); DirectoryInfo destDir = new DirectoryInfo(destPath); // the source directory must exist, if not raise an exception if (sourceDir.Exists) { // if dest dir does not exist throw an exception if (!destDir.Parent.Exists) new Exception("目的目录不存在: " + destDir.Parent.FullName, new DirectoryNotFoundException()); if (!destDir.Exists) destDir.Create(); // copy all the files of the current directory foreach (FileInfo file in sourceDir.GetFiles()) { if (overwrite) file.CopyTo(Path.Combine(destDir.FullName, file.Name), true); else { // if overwrite = false, copy the file only if it does not exist // this is done to avoid an IOException if a file already exists // this way the other files can be copied anyway... if (!File.Exists(Path.Combine(destDir.FullName, file.Name))) file.CopyTo(Path.Combine(destDir.FullName, file.Name), false); } }
// copy all the sub-directories by recursively calling this same routine foreach (DirectoryInfo dir in sourceDir.GetDirectories()) CopyDirectory(dir.FullName, Path.Combine(destDir.FullName, dir.Name), overwrite); } else new AppException("原目录不存在: " + sourceDir.FullName, new DirectoryNotFoundException()); }
try{
Directory.CreateDirectory(dest);
String[] sub = Directory.GetDirectories(src);
for (int i = 0; i < sub.Length; i++) {
String name = Path.GetFileName(sub[i]);
Copy(sub[i], dest + "\\" + name);
}
String[] files = Directory.GetFiles(src);
for (int i = 0; i < files.Length; i++) {
String name = Path.GetFileName(files[i]);
File.Copy(files[i], dest + "\\" + name);
}
}catch(Exception e){
Console.WriteLine(e);
}
}经过测试完全可以,但是如果出错的话,你可以加些错误处理。
WindowsAPI里恳定有,实在不行也可以用ShellExcute()呀。
要不就生成一个batch文件一样是可以的,这都是些懒方法,如果你愿意,当然也可以像楼上那位。
{
DirectoryInfo sourceDir = new DirectoryInfo(sourcePath);
DirectoryInfo destDir = new DirectoryInfo(destPath); // the source directory must exist, if not raise an exception
if (sourceDir.Exists)
{
// if dest dir does not exist throw an exception
if (!destDir.Parent.Exists)
new Exception("目的目录不存在: " + destDir.Parent.FullName,
new DirectoryNotFoundException()); if (!destDir.Exists) destDir.Create(); // copy all the files of the current directory
foreach (FileInfo file in sourceDir.GetFiles())
{
if (overwrite)
file.CopyTo(Path.Combine(destDir.FullName, file.Name), true);
else
{
// if overwrite = false, copy the file only if it does not exist
// this is done to avoid an IOException if a file already exists
// this way the other files can be copied anyway...
if (!File.Exists(Path.Combine(destDir.FullName, file.Name)))
file.CopyTo(Path.Combine(destDir.FullName, file.Name), false);
}
}
// copy all the sub-directories by recursively calling this same routine
foreach (DirectoryInfo dir in sourceDir.GetDirectories())
CopyDirectory(dir.FullName, Path.Combine(destDir.FullName, dir.Name), overwrite);
}
else
new AppException("原目录不存在: " + sourceDir.FullName,
new DirectoryNotFoundException());
}
system("xcopy sourcedir destdir /E /C /I /Q");