SFTPHelper

  1 public class SFTPHelper
  2     {
  3         #region 字段或属性
  4 
  5         private readonly SftpClient _sftp;
  6 
  7         /// <summary>
  8         /// SFTP连接状态
  9         /// </summary>
 10         public bool Connected
 11         {
 12             get
 13             {
 14                 return _sftp.IsConnected;
 15             }
 16         }
 17 
 18         #endregion
 19 
 20         #region 构造函数
 21 
 22         /// <summary>
 23         /// 构造1
 24         /// </summary>
 25         /// <param name="ip">IP</param>
 26         /// <param name="port">端口</param>
 27         /// <param name="user">用户名</param>
 28         /// <param name="pwd">密码</param>
 29         public SFTPHelper(string ip, string port, string user, string pwd)
 30         {
 31             _sftp = new SftpClient(ip, Int32.Parse(port), user, pwd);
 32         }
 33 
 34         /// <summary>
 35         /// 构造2
 36         /// </summary>
 37         /// <param name="host">服务器地址</param>
 38         /// <param name="user">用户名</param>
 39         /// <param name="pwd">密码</param>
 40         public SFTPHelper(string host, string user, string pwd)
 41         {
 42             _sftp = new SftpClient(host, user, pwd);
 43         }
 44 
 45         #endregion
 46 
 47         #region 连接SFTP
 48 
 49         /// <summary>
 50         /// 连接SFTP
 51         /// </summary>
 52         /// <returns>true成功</returns>
 53         private void Connect()
 54         {
 55             if (!Connected)
 56             {
 57                 _sftp.Connect();
 58             }
 59         }
 60 
 61         #endregion
 62 
 63         #region 断开SFTP
 64 
 65         /// <summary>
 66         /// 断开SFTP
 67         /// </summary> 
 68         private void Disconnect()
 69         {
 70             if (_sftp != null && Connected)
 71             {
 72                 _sftp.Disconnect();
 73             }
 74         }
 75 
 76         #endregion
 77 
 78         #region SFTP上传文件
 79 
 80         /// <summary>
 81         /// SFTP上传文件
 82         /// </summary>
 83         /// <param name="localPath">本地路径</param>
 84         /// <param name="remotePath">远程路径</param>
 85         public void UploadFile(string localPath, string remotePath)
 86         {
 87             using (var fileStream = File.OpenRead(localPath))
 88             {
 89                 Connect();
 90                 _sftp.UploadFile(fileStream, remotePath);
 91                 Disconnect();
 92             }
 93         }
 94 
 95         #endregion
 96 
 97         #region SFTP下载文件
 98 
 99         /// <summary>
100         /// SFTP下载文件
101         /// </summary>
102         /// <param name="remotePath">远程路径</param>
103         /// <param name="localPath">本地路径</param>
104         public void DownloadFile(string remotePath, string localPath)
105         {
106             Connect();
107             var byt = _sftp.ReadAllBytes(remotePath);
108             Disconnect();
109             File.WriteAllBytes(localPath, byt);
110         }
111 
112         #endregion
113     }

 

转载于:https://www.cnblogs.com/JustYong/p/4441838.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值