Sftp is an upgraded version of the ftp protocol, which sacrifices upload speed in exchange for security performance. I have started to try using it, but it does not support the new version of openssh. All methods of adoption require dependencies:Download link
/// <summary> /// SFTP operation class /// </summary> public class SFTPHelper { #region field or property private SftpClient sftp; /// <summary> /// SFTP connection status /// </summary> public bool Connected { get { return ; } } #endregion #region construct /// <summary> /// Construct /// </summary> /// <param name="ip">IP</param> /// <param name="port">port</param> /// <param name="user">username</param> /// <param name="pwd">Password</param> public SFTPHelper(string ip, string port, string user, string pwd) { sftp = new SftpClient(ip, (port), user, pwd); } #endregion #region Connect to SFTP /// <summary> /// Connect to SFTP /// </summary> /// <returns>true success</returns> public bool Connect() { try { if (!Connected) { (); } return true; } catch (Exception ex) { // ((), ("Connecting SFTP failed, reason: {0}", )); throw new Exception(("connectSFTPfail,reason:{0}", )); } } #endregion #region Disconnect SFTP /// <summary> /// Disconnect SFTP /// </summary> public void Disconnect() { try { if (sftp != null && Connected) { (); } } catch (Exception ex) { // ((), ("Disconnect SFTP failed, reason: {0}", )); throw new Exception(("disconnectSFTPfail,reason:{0}", )); } } #endregion #region SFTP upload file /// <summary> /// SFTP upload file /// </summary> /// <param name="localPath">local path</param> /// <param name="remotePath">Remote Path</param> public void Put(string localPath, string remotePath) { try { using (var file = (localPath)) { Connect(); (file, remotePath); Disconnect(); } } catch (Exception ex) { // ((), ("SFTP file upload failed, reason: {0}", )); throw new Exception(("SFTP文件上传fail,reason:{0}", )); } } #endregion #region SFTP get file /// <summary> /// SFTP gets the file /// </summary> /// <param name="remotePath">Remote Path</param> /// <param name="localPath">local path</param> public void Get(string remotePath, string localPath) { try { Connect(); var byt = (remotePath); Disconnect(); (localPath, byt); } catch (Exception ex) { // ((), ("SFTP file acquisition failed, reason: {0}", )); throw new Exception(("SFTP文件获取fail,reason:{0}", )); } } #endregion #region Delete SFTP file /// <summary> /// Delete SFTP file /// </summary> /// <param name="remoteFile">Remote Path</param> public void Delete(string remoteFile) { try { Connect(); (remoteFile); Disconnect(); } catch (Exception ex) { // ((), ("SFTP file deletion failed, reason: {0}", )); throw new Exception(("SFTP文件删除fail,reason:{0}", )); } } #endregion #region Get the list of SFTP files /// <summary> /// Get the list of SFTP files /// </summary> /// <param name="remotePath">Remote Directory</param> /// <param name="fileSuffix">File suffix</param> /// <returns></returns> public ArrayList GetFileList(string remotePath, string fileSuffix) { try { Connect(); var files = (remotePath); Disconnect(); var objList = new ArrayList(); foreach (var file in files) { string name = ; if ( > ( + 1) && fileSuffix == ( - )) { (name); } } return objList; } catch (Exception ex) { // ((), ("SFTP file list acquisition failed, reason: {0}", )); throw new Exception(("SFTP文件列表获取fail,reason:{0}", )); } } #endregion #region Mobile SFTP file /// <summary> /// Move SFTP files /// </summary> /// <param name="oldRemotePath">Old remote path</param> /// <param name="newRemotePath">New remote path</param> public void Move(string oldRemotePath, string newRemotePath) { try { Connect(); (oldRemotePath, newRemotePath); Disconnect(); } catch (Exception ex) { // ((), ("SFTP file movement failed, reason: {0}", )); throw new Exception(("SFTP文件移动fail,reason:{0}", )); } } #endregion }
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.