- using System.IO;
- using System.Collections;
- using System.Collections.Generic;
- string copyfrompath=@"D:\\7z920-CP";
- string copytopath ="C:";
- copytopath = (copytopath.LastIndexOf(Path.DirectorySeparatorChar) == copytopath.Length - 1) ? copytopath : (copytopath+Path.DirectorySeparatorChar) + Path.GetFileName(copyfrompath);
- string targetpath;
- int num=1;
- for(targetpath=copytopath+"(test version "+num+++")";Directory.Exists(targetpath);targetpath=copytopath+"(test version "+num+++")");
- Queue<FileSystemInfo> copyfolders = new Queue<FileSystemInfo>(new DirectoryInfo(copyfrompath).GetFileSystemInfos());
- Directory.CreateDirectory(targetpath);
- int sourcedirlen=copyfrompath.Length;
- while (copyfolders.Count>0)
- {
- FileSystemInfo atom = copyfolders.Dequeue();
- FileInfo file = atom as FileInfo;
- if (file == null)
- {
- DirectoryInfo directory = atom as DirectoryInfo;
- Directory.CreateDirectory(targetpath+directory.FullName.Substring(sourcedirlen));
- foreach (FileSystemInfo fi in directory.GetFileSystemInfos())
- copyfolders.Enqueue(fi);
- }
- else
- file.CopyTo(targetpath+file.FullName.Substring(sourcedirlen));
- }
- //该片段来自于http://www.codesnippet.cn/detail/0307201513004.html
来源: http://www.codesnippet.cn/detail/0307201513004.html