C# FTP下载 采用Ssh.Net方式

不要再用FTPClient了

nuget下载Ssh.Net

然后代码如下:

cs 复制代码
/// <summary>
  /// SFTP操作类
  /// </summary>
  public class SFTPHelper
  {
    #region 字段或属性
    private SftpClient sftp;
    /// <summary>
    /// SFTP连接状态
    /// </summary>
    public bool Connected { get { return sftp.IsConnected; } }
    #endregion

    #region 构造
    /// <summary>
    /// 构造
    /// </summary>
    /// <param name="ip">IP</param>
    /// <param name="port">端口</param>
    /// <param name="user">用户名</param>
    /// <param name="pwd">密码</param>
    public SFTPHelper(string ip, string port, string user, string pwd)
    {
      sftp = new SftpClient(ip, Int32.Parse(port), user, pwd);
    }
    #endregion

    #region 连接SFTP
    /// <summary>
    /// 连接SFTP
    /// </summary>
    /// <returns>true成功</returns>
    public bool Connect()
    {
      try
      {
        if (!Connected)
        {
          sftp.Connect();
        }
        return true;
      }
      catch (Exception ex)
      {
        // TxtLog.WriteTxt(CommonMethod.GetProgramName(), string.Format("连接SFTP失败,原因:{0}", ex.Message));
        throw new Exception(string.Format("连接SFTP失败,原因:{0}", ex.Message));
      }
    }
    #endregion

    #region 断开SFTP
    /// <summary>
    /// 断开SFTP
    /// </summary> 
    public void Disconnect()
    {
      try
      {
        if (sftp != null && Connected)
        {
          sftp.Disconnect();
        }
      }
      catch (Exception ex)
      {
        // TxtLog.WriteTxt(CommonMethod.GetProgramName(), string.Format("断开SFTP失败,原因:{0}", ex.Message));
        throw new Exception(string.Format("断开SFTP失败,原因:{0}", ex.Message));
      }
    }
    #endregion

    #region SFTP上传文件
    /// <summary>
    /// SFTP上传文件
    /// </summary>
    /// <param name="localPath">本地路径</param>
    /// <param name="remotePath">远程路径</param>
    public void Put(string localPath, string remotePath)
    {
      try
      {
        using (var file = File.OpenRead(localPath))
        {
          Connect();
          sftp.UploadFile(file, remotePath);
          Disconnect();
        }
      }
      catch (Exception ex)
      {
        // TxtLog.WriteTxt(CommonMethod.GetProgramName(), string.Format("SFTP文件上传失败,原因:{0}", ex.Message));
        throw new Exception(string.Format("SFTP文件上传失败,原因:{0}", ex.Message));
      }
    }
    #endregion

    #region SFTP获取文件
    /// <summary>
    /// SFTP获取文件
    /// </summary>
    /// <param name="remotePath">远程路径</param>
    /// <param name="localPath">本地路径</param>
    public void Get(string remotePath, string localPath)
    {
      try
      {
        Connect();
        var byt = sftp.ReadAllBytes(remotePath);
        Disconnect();
        File.WriteAllBytes(localPath, byt);
      }
      catch (Exception ex)
      {
        // TxtLog.WriteTxt(CommonMethod.GetProgramName(), string.Format("SFTP文件获取失败,原因:{0}", ex.Message));
        throw new Exception(string.Format("SFTP文件获取失败,原因:{0}", ex.Message));
      }

    }
    #endregion

    #region 删除SFTP文件
    /// <summary>
    /// 删除SFTP文件 
    /// </summary>
    /// <param name="remoteFile">远程路径</param>
    public void Delete(string remoteFile)
    {
      try
      {
        Connect();
        sftp.Delete(remoteFile);
        Disconnect();
      }
      catch (Exception ex)
      {
        // TxtLog.WriteTxt(CommonMethod.GetProgramName(), string.Format("SFTP文件删除失败,原因:{0}", ex.Message));
        throw new Exception(string.Format("SFTP文件删除失败,原因:{0}", ex.Message));
      }
    }
    #endregion

    #region 获取SFTP文件列表
    /// <summary>
    /// 获取SFTP文件列表
    /// </summary>
    /// <param name="remotePath">远程目录</param>
    /// <param name="fileSuffix">文件后缀</param>
    /// <returns></returns>
    public ArrayList GetFileList(string remotePath, string fileSuffix)
    {
      try
      {
        Connect();
        var files = sftp.ListDirectory(remotePath);
        Disconnect();
        var objList = new ArrayList();
        foreach (var file in files)
        {
          string name = file.Name;
          if (name.Length > (fileSuffix.Length + 1) && fileSuffix == name.Substring(name.Length - fileSuffix.Length))
          {
            objList.Add(name);
          }
        }
        return objList;
      }
      catch (Exception ex)
      {
        // TxtLog.WriteTxt(CommonMethod.GetProgramName(), string.Format("SFTP文件列表获取失败,原因:{0}", ex.Message));
        throw new Exception(string.Format("SFTP文件列表获取失败,原因:{0}", ex.Message));
      }
    }
    #endregion

    #region 移动SFTP文件
    /// <summary>
    /// 移动SFTP文件
    /// </summary>
    /// <param name="oldRemotePath">旧远程路径</param>
    /// <param name="newRemotePath">新远程路径</param>
    public void Move(string oldRemotePath, string newRemotePath)
    {
      try
      {
        Connect();
        sftp.RenameFile(oldRemotePath, newRemotePath);
        Disconnect();
      }
      catch (Exception ex)
      {
        // TxtLog.WriteTxt(CommonMethod.GetProgramName(), string.Format("SFTP文件移动失败,原因:{0}", ex.Message));
        throw new Exception(string.Format("SFTP文件移动失败,原因:{0}", ex.Message));
      }
    }
    #endregion

  }

调用:

cs 复制代码
   SFTPHelper sFTPHelper = new SFTPHelper("172.0.0.1", "21", "sftp", "bacd123");
   sFTPHelper.Get("/bak/10001.pdf", "D:\\10001.pdf");
相关推荐
EdisonZhou20 分钟前
大模型应用开发初探 : 通用函数调用Planner
aigc·.net·.net core
IT规划师22 分钟前
C#|.net core 基础 - 扩展数组添加删除性能最好的方法
c#·.netcore·数组
时光追逐者1 小时前
分享6个.NET开源的AI和LLM相关项目框架
人工智能·microsoft·ai·c#·.net·.netcore
friklogff1 小时前
【C#生态园】提升C#开发效率:深入了解自然语言处理库与工具
开发语言·c#·区块链
__water10 小时前
『功能项目』回调函数处理死亡【54】
c#·回调函数·unity引擎
__water10 小时前
『功能项目』眩晕图标显示【52】
c#·unity引擎·动画事件
__water11 小时前
『功能项目』第二职业法师的平A【57】
c#·unity引擎·魔法球伤害传递
Java资深爱好者12 小时前
VB.NET中如何利用ASP.NET进行Web开发
前端·asp.net·.net
__water13 小时前
『功能项目』战士的伤害型技能【45】
c#·unity引擎·战士职业伤害型技能
君莫愁。14 小时前
【Unity】检测鼠标点击位置是否有2D对象
unity·c#·游戏引擎