Example #1
0
        /// <summary>
        /// 執行更新資料夾
        /// </summary>
        /// <param name="sourceRoot">Copy檔案的來源位置(//127.0.0.1/)</param>
        /// <param name="targetRoot">Copy檔案到的目標端(//192.168.166.1/)</param>
        /// <param name="copyFolderList">要Copy的目錄列表(沒設定即Copy整個資料夾內的資料)</param>
        /// <param name="sourceAccount">來源位置帳號</param>
        /// <param name="sourcePassword">來源位置密碼</param>
        /// <param name="targetAccount">目標位置帳號</param>
        /// <param name="targetPassword">目標位置密碼</param>
        /// <param name="compressPassword">壓縮密碼</param>
        /// <param name="delExtraFile">是否要刪除額外的檔案</param>
        /// <param name="delExtraFolder">是否要刪除額外的資料夾</param>
        public static void DoFolderCopy(string sourceRoot, string targetRoot, List <string> copyFolderList, string sourceAccount, string sourcePassword, string targetAccount, string targetPassword, string compressPassword, bool delExtraFile, bool delExtraFolder)
        {
            SourceRoot        = sourceRoot;
            TargetRoot        = targetRoot;
            GetCopyFolderList = copyFolderList;
            IsDelExtraFile    = delExtraFile;
            IsDelExtraFolder  = delExtraFolder;
            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");
            if (targetRoot.Contains(sourceRoot))
            {
                backupFolder = targetRoot.Replace(sourceRoot + "\\", string.Empty).TrimStart().
                               Substring(0, targetRoot.Replace(sourceRoot + "\\", string.Empty).TrimStart().IndexOf("\\"));
            }
            if (string.IsNullOrEmpty(compressPassword))
            {
                CopyFolder(SourceRoot, 0);
            }
            else
            {
                CompressAndCopyFolder(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();
            }
        }
Example #2
0
        /// <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();
            }
        }