开发手册 欢迎您!
软件开发者资料库

(C#)SSH.NET sftp常用操作工具类代码(连接,上传,下载,删除,移动)

SSH.NET是用于.NET的Secure Shell(SSH-2)库,sftp是通过ssh实现的,本文主要介绍.Net(C#)中,使用SSH.NET对sftp常用操作及工具类代码,包括sftp服务器的连接,文件上传,下载文件,文件删除,文件的位置的移动等操作。

1、SSH.NET安装引用

使用Nuget管理工具搜索"SSH.NET"=>找到选择安装

相关文档VS(Visual Studio)中Nuget的使用

2、SSH.NET操作代码

using Renci.SshNet;using Renci.SshNet.Sftp;using System;using System.Collections;using System.Collections.Generic;using System.IO;namespace ConsoleApplication1{public class SFtp{private SftpClient sftp;        ///         /// 连接状态        /// public bool Connected{get{return this.sftp.IsConnected;}}        ///         ///         ///         /// sftp的IP        /// sftp的端口        /// sftp的帐户        /// sftp的密码public SFtp(string host, int port, string username, string password){this.sftp = new SftpClient(host, port, username, password);}        ///         /// 连接sftp服务器        ///         /// 连接状态public bool Connect(){bool result;try{bool flag = !this.Connected;if (flag){this.sftp.Connect();}result = true;}catch (Exception ex){throw new Exception(string.Format("连接SFTP失败,原因:{0}", ex.Message));}return result;}        ///         /// 断开连接        /// public void Disconnect(){try{bool flag = this.sftp != null && this.Connected;if (flag){this.sftp.Disconnect();}}catch (Exception ex){throw new Exception(string.Format("断开SFTP失败,原因:{0}", ex.Message));}}        ///         /// 上传文件        ///         /// 本地文件路径        /// 服务器端文件路径public void Put(string localPath, string remotePath){try{using (FileStream fileStream = File.OpenRead(localPath)){this.Connect();this.sftp.UploadFile(fileStream, remotePath, null);this.Disconnect();}}catch (Exception ex){throw new Exception(string.Format("SFTP文件上传失败,原因:{0}", ex.Message));}}        ///         /// 上传文字字节数据        ///         /// 文件内容字节        /// 上传到服务器的路径public void Put(byte[] fileByteArr, string remotePath){try{Stream input = new MemoryStream(fileByteArr);this.Connect();this.sftp.UploadFile(input, remotePath, null);this.Disconnect();}catch (Exception ex){throw new Exception(string.Format("SFTP文件上传失败,原因:{0}", ex.Message));}}        ///         /// 将sftp服务器的文件下载本地        ///         /// 服务器上的路径        /// 本地的路径public void Get(string remotePath, string localPath){try{this.Connect();byte[] bytes = this.sftp.ReadAllBytes(remotePath);this.Disconnect();File.WriteAllBytes(localPath, bytes);}catch (Exception ex){throw new Exception(string.Format("SFTP文件获取失败,原因:{0}", ex.Message));}}        ///         ///  删除ftp服务器上的文件        ///         /// 服务器上的路径public void Delete(string remoteFile){try{this.Connect();this.sftp.Delete(remoteFile);this.Disconnect();}catch (Exception ex){throw new Exception(string.Format("SFTP文件删除失败,原因:{0}", ex.Message));}}        ///         /// 获取ftp服务器上指定路径上的指定前缀的文件名列表        ///         /// 服务器上的路径        /// 文件名前缀        ///         public List GetFileList(string remotePath, string fileSuffix){List result;try{this.Connect();IEnumerable enumerable = this.sftp.ListDirectory(remotePath, null);this.Disconnect();                result = new List();foreach (SftpFile current in enumerable){string name = current.Name;bool flag = name.Length > fileSuffix.Length + 1 && fileSuffix == name.Substring(name.Length - fileSuffix.Length);if (flag){result.Add(name);}}}catch (Exception ex){throw new Exception(string.Format("SFTP文件列表获取失败,原因:{0}", ex.Message));}return result;}        ///         /// ftp服务器端文件移动        ///         /// 原来服务器上路径        /// 移动后服务器上新路径public void Move(string oldRemotePath, string newRemotePath){try{this.Connect();this.sftp.RenameFile(oldRemotePath, newRemotePath);this.Disconnect();}catch (Exception ex){throw new Exception(string.Format("SFTP文件移动失败,原因:{0}", ex.Message));}}}}

相关文档.NET Core通过SSH.NET实现sftp通过ssh上传下载文件代码