C#實現伺服器間檔案同步

罪後憶支湮發表於2015-07-09
using System.IO;


        /// <summary>
        /// 遠端登陸伺服器
        /// </summary>
        /// <param name="remoteHost">遠端伺服器IP路徑</param>
        /// <param name="userName"></param>
        /// <param name="passWord"></param>
        /// <returns>是否登陸成功</returns>
        public static bool ConnectServer(string remoteHost, string userName, string passWord)
        {
            bool Flag = false;
            Process proc = new Process();
            try
            {
                proc.StartInfo.FileName = "cmd.exe";
                proc.StartInfo.UseShellExecute = false;
                proc.StartInfo.RedirectStandardInput = true;
                proc.StartInfo.RedirectStandardOutput = true;
                proc.StartInfo.RedirectStandardError = true;
                proc.StartInfo.CreateNoWindow = true;
                proc.Start();
                string dosLine = "net use " + remoteHost + " " + passWord + " /user:" + userName;
                proc.StandardInput.WriteLine(dosLine);
                proc.StandardInput.WriteLine("exit");
                while (!proc.HasExited)
                {
                    proc.WaitForExit(1000);
                }
                proc.StandardError.Close();

                Flag = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                proc.Close();
                proc.Dispose();
            }

            return Flag;
        }
        //Connect(@"\\192.168.15.90", "administrator", "123")//連線伺服器



        /// <summary>
        /// 遞迴-拷貝資料夾及檔案
        /// </summary>
        /// <param name="sourceDirectoryUrl">源路徑</param>
        /// <param name="copyDirectoryUrl">複製路徑</param>
        private void CopyDirectory(string sourceDirectoryUrl, string copyDirectoryUrl)
        {
            try
            {
                string folderName = sourceDirectoryUrl.Substring(sourceDirectoryUrl.LastIndexOf("\\") + 1);

                string desfolderdir = copyDirectoryUrl + "\\" + folderName;

                if (copyDirectoryUrl.LastIndexOf("\\") == (copyDirectoryUrl.Length - 1))
                {
                    desfolderdir = copyDirectoryUrl + folderName;
                }

                string[] filenames = Directory.GetFileSystemEntries(sourceDirectoryUrl);

                foreach (string file in filenames)// 遍歷所有的檔案和目錄
                {
                    if (Directory.Exists(file))// 先當作目錄處理如果存在這個目錄就遞迴Copy該目錄下面的檔案
                    {

                        string currentdir = desfolderdir + "\\" + file.Substring(file.LastIndexOf("\\") + 1);
                        if (!Directory.Exists(currentdir))
                        {
                            Directory.CreateDirectory(currentdir);
                        }

                        CopyDirectory(file, desfolderdir);
                    }
                    else // 否則直接copy檔案
                    {
                        string srcfileName = file.Substring(file.LastIndexOf("\\") + 1);

                        srcfileName = desfolderdir + "\\" + srcfileName;

                        if (!Directory.Exists(desfolderdir))
                        {
                            Directory.CreateDirectory(desfolderdir);
                        }

                        File.Copy(file, srcfileName, true);

                    }
                }//foreach 

                MessageBox.Show("檔案同步成功!");

            }
            catch(Exception ex)
            {
                MessageBox.Show("檔案同步失敗! 錯誤提示:"+ex.Message);
            }
            finally
            {
                this.Dispose();
                this.Close();
            }

           

        }

        //檔案同步按鈕
        private void synchronization_Btn_Click(object sender, EventArgs e)
        {
            //連線遠端伺服器
            if (ConnectServer(@"\\192.168.0.150\檔案同步測試區", "lgq0821", "123456"))
            {
                //同步檔案
                CopyDirectory(@"F:\檔案同步測試區\A伺服器\工具包", @"\\192.168.0.150\檔案同步測試區");
            }
            else
            {
                MessageBox.Show("身份驗證失敗!");
            }

        }

 

相關文章