基本信息
源码名称:sftp实例,SecureFileTransferProtocol 安全文件传送协议的用法与实例 附完整源码下载
源码大小:0.43M
文件格式:.rar
开发语言:C#
更新时间:2013-05-24
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Renci.SshNet; using System.IO; using System.Threading; namespace SFTP_VS { public class SFtpClient : IFtpClient { SftpClient sftp = null; /// <summary> /// 构造函数 /// </summary> /// <param name="host">sftp服务器名或IP</param> /// <param name="port">端口,默认22</param> /// <param name="user"></param> /// <param name="privateKey"></param> /// <param name="passPhrase"></param> public SFtpClient(string host, int? port, string user, string privateKey, string passPhrase) { PrivateKeyFile keyFile = null; if (string.IsNullOrEmpty(passPhrase)) { keyFile = new PrivateKeyFile(privateKey); } else { keyFile = new PrivateKeyFile(privateKey, passPhrase); } if (port.HasValue) { sftp = new SftpClient(host, port.Value, user, keyFile); } else { sftp = new SftpClient(host, user, keyFile); } if (sftp != null) { sftp.ConnectionInfo.RetryAttempts = 5; sftp.ConnectionInfo.Timeout = new TimeSpan(0, 3, 0); } } public bool Connect() { if (sftp == null) { return false; } if (sftp.IsConnected) { return true; } try { sftp.Connect(); return true; } catch (Exception ex) { string server = string.Format("{0}:{1}", sftp.ConnectionInfo.Username, sftp.ConnectionInfo.Host); // 我用的是nLog来记录错误日志。 // logger.Error("[{0}] SFTP连接发生错误。", server, ex); return false; } } public void DisConnect() { if (sftp == null) { return; } if (!sftp.IsConnected) { return; } try { sftp.Disconnect(); sftp.Dispose(); sftp = null; } catch (Exception ex) { //logger.Error("SFTP断开连接发生错误。", ex); } } /// <summary> /// 取得文件列表 /// </summary> /// <param name="path">路径</param> /// <returns></returns> public List<string> ListFiles(string path) { if (!Connect()) { return null; } List<string> files = new List<string>(); try { sftp.ChangeDirectory("/"); sftp.ListDirectory(path).ToList().ForEach(f => { files.Add(f.FullName); }); return files; } catch (Exception ex) { // logger.Error("[{0}] 取得文件列表发生错误。", Path, ex); return null; } } /// <summary> /// 下载文件 /// </summary> /// <param name="remoteFileName">包含全路径的服务器端文件名</param> /// <param name="localFileName">本地保存的文件名</param> /// <returns></returns> public bool Download(string remoteFileName, string localFileName) { if (!Connect()) { return false; } try { sftp.ChangeDirectory("/"); FileStream fs = File.OpenWrite(localFileName); sftp.DownloadFile(remoteFileName, fs); fs.Close(); return true; } catch (Exception ex) { //logger.Error("[{0}] 文件下载发生错误。", remoteFileName, ex); return false; } } /// <summary> /// 上传文件 /// </summary> /// <param name="localFileName">待上传的文件</param> /// <param name="remoteFileName">服务器端文件名</param> /// <returns></returns> public bool Upload(string localFileName, string remoteFileName) { if (!Connect()) { return false; } try { sftp.ChangeDirectory("/"); FileStream fs = File.OpenRead(localFileName); sftp.UploadFile(fs, remoteFileName, true); fs.Close(); Thread.Sleep(1000); return true; } catch (Exception ex) { //logger.Error("[{0}] 文件上传发生错误。", localFileName, ex); return false; } } /// <summary> /// 文件改名 /// </summary> /// <param name="localFileName">包含全路径的源文件名</param> /// <param name="remoteFileName">包含全路径的新文件名</param> /// <returns></returns> public bool Rename(string orgFileName, string newFileName) { if (!Connect()) { return false; } try { sftp.ChangeDirectory("/"); sftp.RenameFile(orgFileName, newFileName); return true; } catch (Exception ex) { //logger.Error("[{0}] 文件改名发生错误。", localFileName, ex); return false; } } /// <summary> /// 删除文件 /// </summary> /// <param name="orgFileName"></param> /// <param name="newFileName"></param> /// <returns></returns> public bool Delete(string fileName) { if (!Connect()) { return false; } try { sftp.ChangeDirectory("/"); sftp.DeleteFile(fileName); return true; } catch (Exception ex) { //logger.Error("[{0}] 文件删除发生错误。", localFileName, ex); return false; } } } }