c# 操作 Sftp
来源:互联网 发布:pcl icp源码 编辑:程序博客网 时间:2024/06/07 09:50
先在项目中引用 Renci.SshNet.dll (点击下载)
创建 SFTPHelper.cs 类
代码:
using Renci.SshNet;using System;using System.Collections;using System.Collections.Generic;using System.IO;using System.Linq;using System.Text;using System.Threading.Tasks;namespace CheatingTransmitterCalibration{ /* * 此类为sftp连接操作类 * 使用方法:引用SshNet文件夹下的Renci.SshNet.dll文件,以及此类文件,即可使用 */ public class SFTPOperation { #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 SFTPOperation(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) { 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) { throw new Exception(string.Format("断开SFTP失败,原因:{0}", ex.Message)); } } #endregion #region SFTP上传文件 /// <summary> /// SFTP上传文件 /// </summary> /// <param name="localPath">本地路径</param> /// <param name="remotePath">远程路径</param> public int Put(string localPath, string remotePath) { try { using (var file = File.OpenRead(localPath)) { Connect(); sftp.UploadFile(file, remotePath); Disconnect(); } return 1; } catch (Exception ex) { //throw new Exception(string.Format("SFTP文件上传失败,原因:{0}", ex.Message)); return 0; } } #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) { 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) { 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) { 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) { throw new Exception(string.Format("SFTP文件移动失败,原因:{0}", ex.Message)); } } #endregion }}
阅读全文
0 0
- C# 操作SFTP总结
- c# 操作 Sftp
- C# SFTP
- C# 直接访问sftp进行相应的操作
- C# SFTP数据传输
- C#实现SFTP文件传输
- c# sftp 库 Chilkat
- shell 操作 sftp例子
- sftp文件操作
- JAVA SFTP操作
- .net 操作sftp服务器
- java 操作sftp
- C#利用Tamir.SharpSSH类库实现SFTP(基于ssh)文件操作
- C#利用Renci.SshNet类库实现SFTP协议操作文件
- C#访问SFTP服务器组件
- Java中实现SFTP操作
- scp和sftp常用操作
- Xshell 的上传下载操作 (sftp)
- Spring MVC中各个filter的用法
- 一步搭建openstack
- MVC、MVC框架
- 62. Unique Paths
- PHP数据库类
- c# 操作 Sftp
- App的启动过程(8)surfaceflinger的启动
- RDD算子
- 静态查找的方法:顺序查找、对半查找、分块查找,C++代码实现
- elf静态链接实例
- 2. PagerBottomTabStrip
- Oracle 12c CDB数据库中数据字典架构
- 【CAS】自定义Controller配置
- 同步、异步、阻塞、非阻塞