SFTPHelper

14534 ワード

  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     }