There is such a case, the code is often encountered in a certain part of a certain part of a project with another feature of the project is the same, often encounter situations move the code, I felt the need of such a tool to automatically move
It focuses on C #, I found that although both can copy files and directories, but it, tfs or other source code management tools may not automatically go together, so it is best to move the first move or manual it, the whole project is the case of some new.
Core code below for reference:
This method is a copy of the main body, wherein the root souce is the source, target: object code directory, itemList: a file or directory root of the source with respect to the path
private static void CopyConstant(string source, string target, List<string> itemList) { List<CopyItemDto> listItem = new List<CopyItemDto>(); foreach (var item in itemList) { listItem.Add(new CopyItemDto() { RelativePath = item, SourceAddress = source + @"\" + item, TargetAddress = target + @"\" + item, }); } var index = 1; foreach (var item in listItem) { Console.WriteLine(string.Format("{0} 开始copy:{1},({2}/{3})", DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"), item.RelativePath, index, listItem.Count)); if (File.Exists(item.SourceAddress)) { CopyFileOnly(item.SourceAddress, item.TargetAddress); } else { copyDirectory(item.SourceAddress, item.TargetAddress); } Console.WriteLine(string.Format("{0} 结束copy:{1}", DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"), item.RelativePath)); index++; } }
It refers to a method wherein CopyFileOnly
public static void CopyFileOnly(string souce, string target) { System.IO.FileInfo file = new FileInfo(souce); string destDirectoryFullName = target.Replace(file.Name, ""); if (!System.IO.Directory.Exists(destDirectoryFullName)) { System.IO.Directory.CreateDirectory(destDirectoryFullName); } file.CopyTo(target, true); }
copyDirectory
public static void copyDirectory(string sPath, string dPath) { string[] directories = System.IO.Directory.GetDirectories(sPath); if (!System.IO.Directory.Exists(dPath)) { System.IO.Directory.CreateDirectory(dPath); } System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(sPath); System.IO.DirectoryInfo[] dirs = dir.GetDirectories(); CopyFile(dir, dPath); if (dirs.Length > 0) { foreach (System.IO.DirectoryInfo temDirectoryInfo in dirs) { string sourceDirectoryFullName = temDirectoryInfo.FullName; string destDirectoryFullName = sourceDirectoryFullName.Replace(sPath, dPath); if (!System.IO.Directory.Exists(destDirectoryFullName)) { System.IO.Directory.CreateDirectory(destDirectoryFullName); } CopyFile(temDirectoryInfo, destDirectoryFullName); copyDirectory (sourceDirectoryFullName, destDirectoryFullName); } } }
CopyFile
/// <Summary> /// all files in the directory to the copy destination directory. /// </ Summary> /// <param> source path </ param> /// <param> object path </ param> public static void the CopyFile (System.IO.DirectoryInfo path, String desPath) { String the sourcePath = path.FullName; System.IO.FileInfo for [] Files = path.GetFiles (); the foreach (System.IO.FileInfo for File in Files) { String sourceFileFullName = file.FullName; string destFileFullName = sourceFileFullName.Replace(sourcePath, desPath); file.CopyTo(destFileFullName, true); } }