栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C# > C#教程

C#利用SFTP实现上传下载

C#教程 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C#利用SFTP实现上传下载

sftp是ftp协议的升级版本,是牺牲上传速度为代价,换取安全性能,本人开始尝试使用Tamir.SharpSSH.dll但它对新版本的openssh 不支持,所有采用Ssh.Net方式 需要依赖:Renci.SshNet.dll 下载链接

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

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

    #region 连接SFTP
    /// 
    /// 连接SFTP
    /// 
    /// true成功
    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
    /// 
    /// 断开SFTP
    ///  
    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上传文件
    /// 
    /// SFTP上传文件
    /// 
    /// 本地路径
    /// 远程路径
    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获取文件
    /// 
    /// SFTP获取文件
    /// 
    /// 远程路径
    /// 本地路径
    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文件
    /// 
    /// 删除SFTP文件 
    /// 
    /// 远程路径
    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文件列表
    /// 
    /// 获取SFTP文件列表
    /// 
    /// 远程目录
    /// 文件后缀
    /// 
    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文件
    /// 
    /// 移动SFTP文件
    /// 
    /// 旧远程路径
    /// 新远程路径
    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

  }

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/123173.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号