c#实现复制全部文件夹及文件的代码
代码如下:
private void Copy()
{
System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo("D:\\data");
CopyDirectorysAndFiles("c:\\", dir);
}
private void CopyDirectorysAndFiles(string dest, System.IO.DirectoryInfo srcdir)
{
if (dest.LastIndexOf('\\') != (dest.Length - 1))
{
dest += "\\";
}
string destPath = dest + srcdir.Name + "\\";
if (!System.IO.Directory.Exists(destPath))
{
System.IO.Directory.CreateDirectory(destPath);
}
System.IO.FileInfo[] files = srcdir.GetFiles();
foreach (System.IO.FileInfo file in files)
{
file.CopyTo(destPath+file.Name , true);
}
System.IO.DirectoryInfo[] dirs = srcdir.GetDirectories();
foreach (System.IO.DirectoryInfo dirInfo in dirs)
{
CopyDirectorysAndFiles(destPath , dirInfo);
}
}
欢迎建议!