private void btnFileExists_Click(object sender, EventArgs e) { using (WinSCP.Session session = new WinSCP.Session()) { fingerprint = session.ScanFingerprint(sessionOptions, "MD5"); sessionOptions.SshHostKeyFingerprint = fingerprint; session.Open(sessionOptions); TransferOptions transferOptions = new TransferOptions(); transferOptions.TransferMode = TransferMode.Binary; TransferOperationResult transferResult; if (session.FileExists(@"/c:/sbmon_complete")) { MessageBox.Show("File {0} exists", @"/c:/sbmon_complete"); } else { MessageBox.Show("File does not exist"); } } }
//单线程即可 public static Tuple <int, string> m_fSendFile(m_cRecModel m_mRecModel, string m_sRemoteDirectory, bool m_bTest, LogTyper lt = LogTyper.LogLogger) { int status = 0; string msg = string.Empty; try { ///链接(S)FTP SessionOptions sessionOptions = new SessionOptions(); sessionOptions.Protocol = (Protocol)(int.Parse(m_cConfigConstants.m_sProtocol)); sessionOptions.HostName = m_cConfigConstants.m_sIP; sessionOptions.PortNumber = int.Parse(m_cConfigConstants.m_sPort); sessionOptions.UserName = m_cConfigConstants.m_sUa; sessionOptions.FtpMode = (FtpMode)(int.Parse(m_cConfigConstants.m_sMode)); if (!string.IsNullOrWhiteSpace(m_cConfigConstants.m_sPwd)) { sessionOptions.Password = m_cConfigConstants.m_sPwd; } if (!string.IsNullOrWhiteSpace(m_cConfigConstants.m_sKey)) { sessionOptions.SshHostKeyFingerprint = m_cConfigConstants.m_sKey; } ///放入测试 using (WinSCP.Session session = new WinSCP.Session()) { session.Open(sessionOptions); TransferEventArgs transferEventArgs; TransferOptions transferOptions = new TransferOptions(); transferOptions.TransferMode = TransferMode.Binary; //是否需要创建文件夹,逐级创建 if (!string.IsNullOrWhiteSpace(m_sRemoteDirectory) && !session.FileExists(RemotePath.EscapeFileMask(m_sRemoteDirectory))) { //查找出所有目录 string[] m_lDirectory = m_sRemoteDirectory.Split('/'); string m_sPrefixMulu = string.Empty; foreach (string m_sMulu in m_lDirectory) { if (!string.IsNullOrWhiteSpace(m_sMulu)) { m_sPrefixMulu += $"/{m_sMulu}"; if (!session.FileExists(RemotePath.EscapeFileMask(m_sPrefixMulu))) { session.CreateDirectory(RemotePath.EscapeFileMask(m_sPrefixMulu)); Log.Instance.Debug($"逐级创建(S)FTP目录:“{m_sPrefixMulu}”", lt); } } } } //判断文件是否已经上传 if (!session.FileExists(RemotePath.EscapeFileMask($"{m_sRemoteDirectory}/{System.IO.Path.GetFileName(m_mRecModel.mimiFile)}"))) { //如果为测试,文件不进行删除 transferEventArgs = session.PutFileToDirectory(m_mRecModel.mimiFile, RemotePath.EscapeFileMask(m_sRemoteDirectory), !m_bTest, transferOptions); string m_sErrMsg = string.Join(",", transferEventArgs?.Error?.Message); if (!string.IsNullOrWhiteSpace(m_sErrMsg)) { Log.Instance.Debug(m_sErrMsg, lt); status = 1; msg = m_sErrMsg; } else { status = 0; msg = "录音上传(S)FTP成功"; } } else { status = 0; msg = "录音已存在,略过上传(S)FTP"; if (!m_bTest) { Log.Instance.Debug($"{m_mRecModel.Id}:“{msg}”", lt); } } } } catch (Exception ex) { status = 1; msg = ex.Message; //如果有误 if (!m_bTest) { m_cSQL.m_fSetActionState(m_mRecModel.Id, "3", $"上传(S)FTP错误:{msg}", lt); } //打印一下错误,处理一下此处导致计划任务无法继续的问题 Log.Instance.Debug(ex, lt); } return(new Tuple <int, string>(status, msg)); }
private void UploadFile(string fileToUpload) { lbLogs.Items.Add("File to upload: " + fileToUpload); bpLoadFile.Value = 0; bpLoadFile.DisplayStyle = ProgressBarDisplayText.Percentage; SessionOptions sessionOptions = new SessionOptions { Protocol = Protocol.Ftp, HostName = this.cloudFoldersInfo.data.host, UserName = this.cloudFoldersInfo.data.user, Password = this.cloudFoldersInfo.data.password, }; using (Session session = new Session()) { // Will continuously report progress of transfer session.FileTransferProgress += SessionFileTransferProgress; // Connect session.Open(sessionOptions); // Upload files TransferOptions transferOptions = new TransferOptions(); transferOptions.TransferMode = TransferMode.Binary; var a = session.ListDirectory("/"); this.remoteUserFolderName = a.Files[2].Name; var filePath = "/" + this.remoteUserFolderName + "/" + filename; if (!session.FileExists(filePath)) { TransferOperationResult transferResult; transferResult = session.PutFiles(fileToUpload, "/" + this.remoteUserFolderName + "/*.*", false, transferOptions); transferResult.Check(); if (transferResult.IsSuccess) { bpLoadFile.CustomText = "Succeed!"; } else { bpLoadFile.CustomText = "Failed!"; } } else { bpLoadFile.CustomText = "Succeed!"; bpLoadFile.Update(); } lbLogs.Items.Add("Package has been uploaded!"); bpLoadFile.DisplayStyle = ProgressBarDisplayText.CustomText; } }