/// <summary> /// 壓縮跟備份資料夾 /// </summary> /// <param name="compressPassword">壓縮密碼</param> private static void CompressAndCopyFolder(string compressPassword) { //取得目錄資訊 DirectoryInfo dir = new DirectoryInfo(SourceRoot); //取得該目錄下的所有目錄 DirectoryInfo[] dirInfo = dir.GetDirectories(); if (!Directory.Exists(TargetRoot)) { Directory.CreateDirectory(TargetRoot); } for (int i = 0; i < dirInfo.Length; i++) { //判斷是否有要下載的目錄但這只有在第一層時才判斷 if (IsCopyFolderListExist(dirInfo[i].Name)) { //壓縮備份 LkCompress.ZipDir(SourceRoot + "/" + dirInfo[i].Name, TargetRoot + dirInfo[i].Name, compressPassword); } } }
/// <summary> /// 執行更新檔案 /// </summary> /// <param name="sourceRoot">Copy檔案的來源位置(//127.0.0.1/)</param> /// <param name="targetRoot">Copy檔案到的目標端(//192.168.166.1/)</param> /// <param name="copyFileList">要Copy的檔案列表(請給副檔名</param> /// <param name="sourceAccount">來源位置帳號</param> /// <param name="sourcePassword">來源位置密碼</param> /// <param name="targetAccount">目標位置帳號</param> /// <param name="targetPassword">目標位置密碼</param> /// <param name="compressFileName">壓縮檔名(不用副檔名)</param> /// <param name="compressPassword">壓縮密碼</param> public static void DoFileCopy(string sourceRoot, string targetRoot, List <string> copyFileList, string sourceAccount, string sourcePassword, string targetAccount, string targetPassword, string compressFileName, string compressPassword) { SourceRoot = sourceRoot; TargetRoot = targetRoot; GetCopyFileList = copyFileList; if (string.IsNullOrEmpty(SourceRoot)) { throw new Exception("未設定來源路徑"); } else if (string.IsNullOrEmpty(TargetRoot)) { throw new Exception("未設定目標路徑"); } LkNetUse sourceNetUse = null; LkNetUse targetNetuse = null; if (!string.IsNullOrEmpty(sourceAccount)) { sourceNetUse = new LkNetUse(SourceRoot.Substring(0, (SourceRoot + "/").IndexOf('/', 3)) + "/", sourceAccount, sourcePassword); Console.WriteLine("Connection Source Remote..."); sourceNetUse.Connect(); } if (!string.IsNullOrEmpty(targetAccount)) { targetNetuse = new LkNetUse(TargetRoot.Substring(0, (TargetRoot).IndexOf('/', 3)) + "/", targetAccount, targetPassword); Console.WriteLine("Connection Target Remote..."); targetNetuse.Connect(); } Console.WriteLine("Start Copy"); List <string> fullPathFile = new List <string>(); foreach (string fileRoot in GetCopyFileList) { if (!File.Exists(SourceRoot + "/" + fileRoot)) { throw new Exception("要Copy的「" + SourceRoot + "/" + fileRoot + "」檔案不存在"); } fullPathFile.Add(SourceRoot + "/" + fileRoot); } if (!Directory.Exists(TargetRoot)) { Directory.CreateDirectory(TargetRoot); } if (string.IsNullOrEmpty(compressFileName)) { foreach (string fileRoot in GetCopyFileList) { FileInfo file = new FileInfo(SourceRoot + "/" + fileRoot); file.CopyTo(TargetRoot + file.Name, CheckVersion(TargetRoot + file.Name, file)); } } else { LkCompress.ZipFiles(fullPathFile, TargetRoot + compressFileName, compressPassword); } Console.WriteLine("Complete"); if (!string.IsNullOrEmpty(sourceAccount)) { Console.WriteLine("Disconnection Source Remote"); sourceNetUse.DisConnect(); } if (!string.IsNullOrEmpty(targetAccount)) { Console.WriteLine("Disconnection Target Remote..."); targetNetuse.DisConnect(); } }