前期准备:
1.官网下载:http://winscp.net/eng/docs/lang:chs
官网C#示例:http://winscp.net/eng/docs/library#csharp
当然还有很多,自行选择
2.准备FTP站点
代码:
private bool moveFile(Protocol FtpType, string HostName, string UserName, string Password,int PortNumber,string SshHostKeyFingerprint,string Path) { try { // Setup session options SessionOptions sessionOptions = new SessionOptions(); sessionOptions.Protocol = FtpType; sessionOptions.HostName = HostName; sessionOptions.UserName = UserName; sessionOptions.Password = Password; sessionOptions.PortNumber = PortNumber; if (FtpType == Protocol.Sftp) sessionOptions.SshHostKeyFingerprint = "SshHostKeyFingerprint"; using (Session session = new Session()) { // Connect session.Open(sessionOptions); // Upload files TransferOptions transferOptions = new TransferOptions(); transferOptions.TransferMode = TransferMode.Binary; TransferOperationResult transferResult; string sourcePath = Temp["FilePath"].ToString(); if (!File.Exists(sourcePath)) { RBAppText("警告:文件不存在,已跳过"); return false; } string fileName = System.IO.Path.GetFileNameWithoutExtension(sourcePath); string extension = System.IO.Path.GetExtension(sourcePath); string remotePath = "/" + Path + "/" + fileName + DateTime.Now.ToString("yyyyMMddHHmmss") + extension; transferResult = session.PutFiles(sourcePath, remotePath, true, transferOptions); // Throw on any error transferResult.Check(); //Print results if (transferResult.Transfers.Count < 1) { RBAppText("警告:没有文件需要处理"); } else { foreach (TransferEventArgs transfer in transferResult.Transfers) { RBAppText("提示:文件处理完成:" + transfer.FileName); } } } return true; } catch (Exception e) { RBAppText("错误:" + e.Message); return false; } }