gpt4 book ai didi

c# - 在 C# 中递归复制内容的最佳方法是什么?

转载 作者:可可西里 更新时间:2023-11-01 03:11:57 25 4
gpt4 key购买 nike

使用 C# 和 ASP.NET 将一个文件夹的内容递归复制到另一个文件夹的最佳方法是什么?

最佳答案

你可以试试这个

DirectoryInfo sourcedinfo = new DirectoryInfo(@"E:\source");
DirectoryInfo destinfo = new DirectoryInfo(@"E:\destination");
copy.CopyAll(sourcedinfo, destinfo);

这是完成所有工作的方法:

public void CopyAll(DirectoryInfo source, DirectoryInfo target)
{
try
{
//check if the target directory exists
if (Directory.Exists(target.FullName) == false)
{
Directory.CreateDirectory(target.FullName);
}

//copy all the files into the new directory

foreach (FileInfo fi in source.GetFiles())
{
fi.CopyTo(Path.Combine(target.ToString(), fi.Name), true);
}


//copy all the sub directories using recursion

foreach (DirectoryInfo diSourceDir in source.GetDirectories())
{
DirectoryInfo nextTargetDir = target.CreateSubdirectory(diSourceDir.Name);
CopyAll(diSourceDir, nextTargetDir);
}
//success here
}
catch (IOException ie)
{
//handle it here
}
}

我希望这会有所帮助:)

关于c# - 在 C# 中递归复制内容的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/627504/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com