我需要将整个目录 C:\X 复制到 C:\Y\X,并且我还需要复制子文件夹。
有没有办法使用 System.IO.File\Directory 命名空间来做到这一点?
感谢所有帮助者!
问问题
11709 次
3 回答
8
此类将复制或移动文件夹,无需递归调用。
这些方法使用自己的堆栈来处理递归,这是为了避免StackOverflowException。
public static class CopyFolder
{
public static void CopyDirectory(string source, string target)
{
var stack = new Stack<Folders>();
stack.Push(new Folders(source, target));
while (stack.Count > 0)
{
var folders = stack.Pop();
Directory.CreateDirectory(folders.Target);
foreach (var file in Directory.GetFiles(folders.Source, "*.*"))
{
string targetFile = Path.Combine(folders.Target, Path.GetFileName(file));
if (File.Exists(targetFile)) File.Delete(targetFile);
File.Copy(file, targetFile);
}
foreach (var folder in Directory.GetDirectories(folders.Source))
{
stack.Push(new Folders(folder, Path.Combine(folders.Target, Path.GetFileName(folder))));
}
}
}
public static void MoveDirectory(string source, string target)
{
var stack = new Stack<Folders>();
stack.Push(new Folders(source, target));
while (stack.Count > 0)
{
var folders = stack.Pop();
Directory.CreateDirectory(folders.Target);
foreach (var file in Directory.GetFiles(folders.Source, "*.*"))
{
string targetFile = Path.Combine(folders.Target, Path.GetFileName(file));
if (File.Exists(targetFile)) File.Delete(targetFile);
File.Move(file, targetFile);
}
foreach (var folder in Directory.GetDirectories(folders.Source))
{
stack.Push(new Folders(folder, Path.Combine(folders.Target, Path.GetFileName(folder))));
}
}
Directory.Delete(source, true);
}
public class Folders
{
public string Source { get; private set; }
public string Target { get; private set; }
public Folders(string source, string target)
{
Source = source;
Target = target;
}
}
}
于 2010-05-10T08:17:48.030 回答
3
这是从xneurons 博客复制的。
public static void CopyAll(DirectoryInfo source, DirectoryInfo target) {
// Check if the target directory exists, if not, create it.
if (Directory.Exists(target.FullName) == false) {
Directory.CreateDirectory(target.FullName);
}
// Copy each file into it’s new directory.
foreach (FileInfo fi in source.GetFiles()) {
Console.WriteLine(@"Copying {0}\{1}", target.FullName, fi.Name);
fi.CopyTo(Path.Combine(target.ToString(), fi.Name), true);
}
// Copy each subdirectory using recursion.
foreach (DirectoryInfo diSourceSubDir in source.GetDirectories()) {
DirectoryInfo nextTargetSubDir =
target.CreateSubdirectory(diSourceSubDir.Name);
CopyAll(diSourceSubDir, nextTargetSubDir);
}
}
于 2010-05-10T08:11:50.487 回答
2
您可以使用SearchOption.AllDirectories
递归搜索文件夹,您只需要在复制之前创建目录...
// string source, destination; - folder paths
int pathLen = source.Length;
foreach (string dirPath in Directory.GetDirectories(source, "*", SearchOption.AllDirectories))
{
string subPath = dirPath.SubString(pathLen);
string newpath = Path.Combine(destination, subPath);
Directory.CreateDirectory(newpath );
}
foreach (string filePath in Directory.GetFiles(source, "*.*", SearchOption.AllDirectories))
{
string subPath = filePath.SubString(pathLen);
string newpath = Path.Combine(destination, subPath);
File.Copy(filePath, newpath);
}
于 2010-12-15T15:17:03.813 回答