SFTPHelper.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. using Renci.SshNet;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Text;
  7. namespace Bowin.Common.Utility.Ftp
  8. {
  9. /// <summary>
  10. /// SFTP操作类
  11. /// </summary>
  12. public class SFTPHelper
  13. {
  14. #region 字段或属性
  15. private SftpClient sftp;
  16. /// <summary>
  17. /// SFTP连接状态
  18. /// </summary>
  19. public bool Connected { get { return sftp.IsConnected; } }
  20. #endregion
  21. #region 构造
  22. /// <summary>
  23. /// 构造
  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. #endregion
  34. #region 连接SFTP
  35. /// <summary>
  36. /// 连接SFTP
  37. /// </summary>
  38. /// <returns>true成功</returns>
  39. public bool Connect()
  40. {
  41. try
  42. {
  43. if (!Connected)
  44. {
  45. sftp.Connect();
  46. }
  47. return true;
  48. }
  49. catch (Exception ex)
  50. {
  51. throw new Exception(string.Format("连接SFTP失败,原因:{0}", ex.Message));
  52. }
  53. }
  54. #endregion
  55. #region 断开SFTP
  56. /// <summary>
  57. /// 断开SFTP
  58. /// </summary>
  59. public void Disconnect()
  60. {
  61. try
  62. {
  63. if (sftp != null && Connected)
  64. {
  65. sftp.Disconnect();
  66. }
  67. }
  68. catch (Exception ex)
  69. {
  70. throw new Exception(string.Format("断开SFTP失败,原因:{0}", ex.Message));
  71. }
  72. }
  73. #endregion
  74. #region SFTP上传文件
  75. /// <summary>
  76. /// SFTP上传文件
  77. /// </summary>
  78. /// <param name="localPath">本地路径</param>
  79. /// <param name="remotePath">远程路径</param>
  80. public void Put(string localPath, string remotePath)
  81. {
  82. try
  83. {
  84. using (var file = File.OpenRead(localPath))
  85. {
  86. Connect();
  87. sftp.UploadFile(file, remotePath);
  88. Disconnect();
  89. }
  90. }
  91. catch (Exception ex)
  92. {
  93. throw new Exception(string.Format("SFTP文件上传失败,原因:{0}", ex.Message));
  94. }
  95. }
  96. #endregion
  97. #region SFTP获取文件
  98. /// <summary>
  99. /// SFTP获取文件
  100. /// </summary>
  101. /// <param name="remotePath">远程路径</param>
  102. /// <param name="localPath">本地路径</param>
  103. public void Get(string remotePath, string localPath)
  104. {
  105. try
  106. {
  107. Connect();
  108. var byt = sftp.ReadAllBytes(remotePath);
  109. Disconnect();
  110. File.WriteAllBytes(localPath, byt);
  111. }
  112. catch (Exception ex)
  113. {
  114. throw new Exception(string.Format("SFTP文件获取失败,原因:{0}", ex.Message));
  115. }
  116. }
  117. #endregion
  118. #region 删除SFTP文件
  119. /// <summary>
  120. /// 删除SFTP文件
  121. /// </summary>
  122. /// <param name="remoteFile">远程路径</param>
  123. public void Delete(string remoteFile)
  124. {
  125. try
  126. {
  127. Connect();
  128. sftp.Delete(remoteFile);
  129. Disconnect();
  130. }
  131. catch (Exception ex)
  132. {
  133. throw new Exception(string.Format("SFTP文件删除失败,原因:{0}", ex.Message));
  134. }
  135. }
  136. #endregion
  137. #region 获取SFTP文件列表
  138. /// <summary>
  139. /// 获取SFTP文件列表
  140. /// </summary>
  141. /// <param name="remotePath">远程目录</param>
  142. /// <param name="fileSuffix">文件后缀</param>
  143. /// <returns></returns>
  144. public ArrayList GetFileList(string remotePath, string fileSuffix)
  145. {
  146. try
  147. {
  148. Connect();
  149. var files = sftp.ListDirectory(remotePath);
  150. Disconnect();
  151. var objList = new ArrayList();
  152. foreach (var file in files)
  153. {
  154. string name = file.Name;
  155. if (name.Length > (fileSuffix.Length + 1) && fileSuffix == name.Substring(name.Length - fileSuffix.Length))
  156. {
  157. objList.Add(name);
  158. }
  159. }
  160. return objList;
  161. }
  162. catch (Exception ex)
  163. {
  164. throw new Exception(string.Format("SFTP文件列表获取失败,原因:{0}", ex.Message));
  165. }
  166. }
  167. #endregion
  168. #region 移动SFTP文件
  169. /// <summary>
  170. /// 移动SFTP文件
  171. /// </summary>
  172. /// <param name="oldRemotePath">旧远程路径</param>
  173. /// <param name="newRemotePath">新远程路径</param>
  174. public void Move(string oldRemotePath, string newRemotePath)
  175. {
  176. try
  177. {
  178. Connect();
  179. sftp.RenameFile(oldRemotePath, newRemotePath);
  180. Disconnect();
  181. }
  182. catch (Exception ex)
  183. {
  184. throw new Exception(string.Format("SFTP文件移动失败,原因:{0}", ex.Message));
  185. }
  186. }
  187. #endregion
  188. }
  189. }