当前位置: 移动技术网 > IT编程>开发语言>c# > C#利用SFTP实现上传下载

C#利用SFTP实现上传下载

2019年07月18日  | 移动技术网IT编程  | 我要评论

移动20元封顶卡,青青草原的日常,金砖峰会召开

sftp是ftp协议的升级版本,是牺牲上传速度为代价,换取安全性能,本人开始尝试使用tamir.sharpssh.dll但它对新版本的openssh 不支持,所有采用ssh.net方式 需要依赖:renci.sshnet.dll

/// <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

  }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网