SoFunction
Updated on 2025-03-06

C# uses SFTP to achieve upload and download

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    /// &lt;summary&gt;
    /// Connect to SFTP    /// &lt;/summary&gt;
    /// <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    /// &lt;summary&gt;
    /// Disconnect SFTP    /// &lt;/summary&gt; 
    public void Disconnect()
    {
      try
      {
        if (sftp != null &amp;&amp; Connected)
        {
          ();
        }
      }
      catch (Exception ex)
      {
        // ((), ("Disconnect SFTP failed, reason: {0}", ));        throw new Exception(("disconnectSFTPfail,reason:{0}", ));
      }
    }
    #endregion

    #region SFTP upload file    /// &lt;summary&gt;
    /// SFTP upload file    /// &lt;/summary&gt;
    /// <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    /// &lt;summary&gt;
    /// SFTP gets the file    /// &lt;/summary&gt;
    /// <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    /// &lt;summary&gt;
    /// Delete SFTP file    /// &lt;/summary&gt;
    /// <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    /// &lt;summary&gt;
    /// Get the list of SFTP files    /// &lt;/summary&gt;
    /// <param name="remotePath">Remote Directory</param>    /// <param name="fileSuffix">File suffix</param>    /// &lt;returns&gt;&lt;/returns&gt;
    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 ( &gt; ( + 1) &amp;&amp; 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    /// &lt;summary&gt;
    /// Move SFTP files    /// &lt;/summary&gt;
    /// <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.