public SFTPHelper(SshConnectionInfo connectionInfo, FileTransferEvent onStart, FileTransferEvent onProgress, FileTransferEvent onEnd) : this(connectionInfo) { m_sshCp.OnTransferStart += onStart; m_sshCp.OnTransferProgress += onProgress; m_sshCp.OnTransferEnd += onEnd; }
/// <summary> /// This method will be used to the start the copy process /// </summary> /// <returns></returns> public bool StartCopy() { //Get get a count what is to be transffered. SourceFileFolderCount = Directory.GetFiles(SourcePath, "*.*", SearchOption.AllDirectories).Length; //Create an string of all the directories in the sourcePath. DirectoryInfo sourceDireactory = new(SourcePath); DirectoryInfo[] directories = sourceDireactory.GetDirectories(); //Loop through the array passing the current folder being looked and the targetPath to be updated. foreach (DirectoryInfo folder in directories) { //call the recursive function to copy the files and create the folders. CopyFilesToTartGet(folder.FullName, TargetPath); } //grab al the files transferred over. string[] targetSourceContentsArray = Directory.GetFiles(TargetPath, "*.*", SearchOption.AllDirectories); //This to what ever class is listening to the event FileTransferEvent?.Invoke(this, targetSourceContentsArray); //return false by default return(true); }
public void GetFileBdFromBel(string f, FileTransferEvent a, FileTransferEvent b, FileTransferEvent c) { var e = f; Sftp oSftp = new Sftp(urlSFtp, userSFtp, passSFtp); oSftp.Connect(22); try { oSftp.OnTransferProgress += a; //oSftp.OnTransferProgress += b; oSftp.OnTransferEnd += c; oSftp.Get($"{dirBd}/{f}"); } catch { } oSftp.Close(); }
/// <summary> /// move all file to remote Sync (使用同步的方式将指定文件夹内的所有文件包括之目录的所有文件在保留目录结果的提前向下复制到shh服务器的指定目录) /// </summary> /// <param name="sshCp"></param> /// <param name="LocalFilePath">Local File Path</param> /// <param name="remoteFilePath">remote File Path</param> /// <param name="errMes"></param> /// <param name="reportProcess">report Process </param> /// <param name="reportError">report Error</param> /// <returns></returns> public static bool SshMvAllFileSync(SshTransferProtocolBase sshCp, string LocalFilePath, string remoteFilePath, out string errMes, Action <string> reportProcess, Action <string> reportError) { errMes = null; bool outResult = true; var PutOutReport = new Action <string>((str) => { if (reportProcess != null) { reportProcess(str); } }); var PutOutError = new Action <string>((str) => { if (reportProcess != null) { reportError(str); } }); FileTransferEvent fileTransferStart = new FileTransferEvent((src, dst, transferredBytes, totalBytes, message) => PutOutReport(string.Format("Put file {0} to {1} state:{2}", src, dst, message))); FileTransferEvent fileTransferEnd = new FileTransferEvent((src, dst, transferredBytes, totalBytes, message) => PutOutReport(string.Format("Put file {0} to {1} state:{2}", src, dst, message))); if (!sshCp.Connected) { errMes = "SshTransferProtocolBase not Connected"; PutOutError(errMes); return(false); } FileInfo[] distFIles = FileService.GetAllFiles(LocalFilePath); if (distFIles == null) { errMes = "no file in LocalFilePath"; PutOutError(errMes); return(false); } sshCp.OnTransferStart += fileTransferStart; sshCp.OnTransferEnd += fileTransferEnd; PutOutReport("start Mv"); foreach (FileInfo tempFileInfo in distFIles) { string tempNowPath = remoteFilePath + tempFileInfo.DirectoryName.MyTrimStr(LocalFilePath, null).Replace(@"\", @"/") + @"/" + tempFileInfo.Name; try { sshCp.Put(tempFileInfo.DirectoryName + @"\" + tempFileInfo.Name, tempNowPath); } catch (Exception ex) { PutOutReport(ex.Message); if (ex.Message.Contains("No such file or directory")) { string tempPath = ex.Message; tempPath = tempPath.Remove(0, tempPath.IndexOf(@"/")); tempPath = tempPath.Remove(tempPath.LastIndexOf(@"/")); PutOutReport("Create folder" + tempPath); if (MySshTool.SshFileMkFullDir(sshCp, tempPath)) { try { sshCp.Put(tempFileInfo.DirectoryName + @"\" + tempFileInfo.Name, tempNowPath); } catch (Exception innerEx) { PutOutError(innerEx.Message); PutOutError(string.Format("transfer fail ,skip this file [from {0} to {1}]", tempFileInfo.DirectoryName + @"\" + tempFileInfo.Name, tempNowPath)); outResult = false; } } else { PutOutError(string.Format("create folder Failed,skip this folder [{0}]", tempPath)); outResult = false; } } } } PutOutReport("Mv Complete"); sshCp.OnTransferStart -= fileTransferStart; sshCp.OnTransferEnd -= fileTransferEnd; return(outResult); }