缺點是寫的蠻硬的...
路徑規則是固定死得就那樣,
不知道還能怎樣改會比較好...
主要使用兩個 function 來完成這件事~
要執行的話呼叫 XCopyFile 就好~
/// <summary> /// 複製所有範例檔案至另一目錄 /// </summary> public static void XCopyFile(int buId, string projectName) { string fileUploadPath = System.Configuration.ConfigurationManager.AppSettings["DefaultFilePath"]; // 預設路徑 string sourcePath = string.Format("{0}\\test", fileUploadPath); string destinationPath = string.Format("{0}\\{1}\\{2}", fileUploadPath, buService.GetName(buId).BU_NAME, projectName); // 找到 source 底下所有的檔案 string[] sourceFiles = Directory.GetFiles(sourcePath); string[] sourceDirectories = Directory.GetDirectories(sourcePath); CopyDirectory(sourceFiles, destinationPath); foreach (string folder in sourceDirectories) { string[] subSourceFiles = Directory.GetFiles(folder); CopyDirectory(subSourceFiles, destinationPath + "\\" + folder.Split(new char[] { '\\', ':' }, StringSplitOptions.RemoveEmptyEntries).LastOrDefault()); } RenameProjectXMLFile(destinationPath,projectName); // 重新命名 xml 檔 }
/// <summary> /// 複製單一目錄底下所有檔案 /// </summary> /// <param name="sourceFiles"></param> /// <param name="destinationPath"></param> /// <param name="subFolder"></param> public static void CopyDirectory(string[] sourceFiles, string destinationPath) { CheckFolder(destinationPath); foreach (string item in sourceFiles) { string fileName = item.Split(new char[] { '\\', ':' }, StringSplitOptions.RemoveEmptyEntries).LastOrDefault(); File.Copy(item, destinationPath + "\\" + fileName, true); } }重新命名檔案名稱 ----
public static void RenameProjectXMLFile(string destinationPath, string projectName) { string sampleFileName = "\\TEST.xml"; File.Move(destinationPath + sampleFileName, destinationPath + "\\build_" + projectName + ".xml"); }
沒有留言:
張貼留言