c#如何实现copy目录到另一个目录下面(在线等待,谢谢)

cyd9913 2003-08-22 09:54:12
是要copy不是cut.
用System.IO.Directory或System.IO.DirectoryInfo实现不了。它们只有移动功能。
...全文
52 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
New_bug 2003-08-22
  • 打赏
  • 举报
回复
//sSrcPath :源文件夹 D:\\test
//sDescPath:目标文件夹 E:\\temp
private void CopyDirectory(string sSrcPath,string sDescPath)
{
try
{
if (System.IO.Directory.Exists(sSrcPath))
{
if (!System.IO.Directory.Exists(sDescPath))
System.IO.Directory.CreateDirectory(sDescPath);
}
else
{
return;
}

string[] sSrcDSet = System.IO.Directory.GetDirectories(sSrcPath);
foreach (string sSrcD in sSrcDSet)
{
string sD = sSrcD;
sD = sD.Replace(sSrcPath,sDescPath);
CopyDirectory(sSrcD,sD);
}

string[] sSrcFSet = System.IO.Directory.GetFiles(sSrcPath);
foreach (string sSrcF in sSrcFSet)
{
string sF = sSrcF;
sF = sF.Replace(sSrcPath,sDescPath);
System.IO.File.Copy(sSrcF,sF,true);
}
}
catch (ArgumentNullException)
{
System.Console.WriteLine("Path is a null reference.");
}
catch (System.Security.SecurityException)
{
System.Console.WriteLine("The caller does not have the required permission.");
}
catch (ArgumentException)
{
System.Console.WriteLine("Path is an empty string, contains only white spaces, or contains invalid characters.");
}
catch (System.IO.DirectoryNotFoundException)
{
System.Console.WriteLine("The path encapsulated in the Directory object does not exist.");
}
}

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧