C#实现服务器间文件同步

C#实现服务器间文件同步
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("身份验证失败!");
            }

        }
C#实现服务器间文件同步

C#实现服务器间文件同步

上一篇:有关win10专业版如何退出Microsoft账户自动绑定为管理员


下一篇:手机添加office365邮箱帐号