//开机同步 public static bool syncOnstart(string home, Socket curClientSocket) { string paraHome = home.Substring(0, home.Length - 1); VerifyHandler.verify(curClientSocket); if (!findFileAndSync(paraHome, curClientSocket, paraHome)) { return(false); } // flag = 2 表示已同步完成 VerifyHandler.postMessage(curClientSocket, MessageAssembler.assembleDone()); return(true); }
// 递归查找当前主目录 public static bool findFileAndSync(string home, Socket curClientSocket, string constHome) { string fullPath = null; string paraPath = null; DirectoryInfo Dir = new DirectoryInfo(home); try { // 查找子目录 foreach (DirectoryInfo d in Dir.GetDirectories()) { fullPath = Dir + "\\" + d.ToString(); paraPath = fullPath.Substring(constHome.Length + 1, fullPath.Length - constHome.Length - 1); // flag = 0 时表示同步的是文件夹 string lastWriteTime = Directory.GetLastWriteTime(fullPath).ToString(); byte[] message = MessageAssembler.assembleDir(paraPath, lastWriteTime); VerifyHandler.postMessage(curClientSocket, message); VerifyHandler.verify(curClientSocket); findFileAndSync(fullPath, curClientSocket, constHome); } // 查找文件 foreach (FileInfo f in Dir.GetFiles()) { fullPath = Dir + "\\" + f.ToString(); paraPath = fullPath.Substring(constHome.Length + 1, fullPath.Length - constHome.Length - 1); // flag = 1 时表示同步的是文件 string lastWriteTime = File.GetLastWriteTime(fullPath).ToString(); byte[] message = MessageAssembler.assembleFile(paraPath, lastWriteTime); VerifyHandler.postMessage(curClientSocket, message); if (!VerifyHandler.verify(curClientSocket)) { FileTransferHandler.postFile(curClientSocket, fullPath); VerifyHandler.verify(curClientSocket); } } return(true); } catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); return(false); } }