Skip to content

Instantly share code, notes, and snippets.

@polarstars
Created June 27, 2018 07:35
Show Gist options
  • Save polarstars/b4e936393706ae5a233c99980d195343 to your computer and use it in GitHub Desktop.
Save polarstars/b4e936393706ae5a233c99980d195343 to your computer and use it in GitHub Desktop.
拷贝目录及子目录内容 #C#
public static void CopyDirectory(String sourcePath, String destinationPath)
{
DirectoryInfo info = new DirectoryInfo(sourcePath);
Directory.CreateDirectory(destinationPath);
foreach (FileSystemInfo fsi in info.GetFileSystemInfos())
{
String destName = Path.Combine(destinationPath, fsi.Name);
if (fsi is System.IO.FileInfo) //如果是文件,复制文件
File.Copy(fsi.FullName, destName);
else //如果是文件夹,新建文件夹,递归
{
Directory.CreateDirectory(destName);
CopyDirectory(fsi.FullName, destName);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment