c# 使用Renci.SshNet.dll操作SFTP总结
1、操作类
/// <summary>
/// SFTP操作类
/// </summary>
public class SFTPHelper
{
#region 字段或属性
private SftpClient sftp;
private string cmsimagesIP = ConfigurationManager.AppSettings["cmsimagesIP"].ToString();
private string cmsimagesName = ConfigurationManager.AppSettings["cmsimagesName"].ToString();
private string cmsimagesPwd = ConfigurationManager.AppSettings["cmsimagesPwd"].ToString(); /// <summary>
/// SFTP连接状态
/// </summary>
public bool Connected { get { return sftp.IsConnected; } }
#endregion #region 构造
/// <summary>
/// 构造
/// </summary>
public SFTPHelper()
{
sftp = new SftpClient(cmsimagesIP, , cmsimagesName, cmsimagesPwd);
}
#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, string fileName)
{
try
{
using (var file = File.OpenRead(localPath))
{
Connect();
//判断路径是否存在
if (!sftp.Exists(remotePath))
{
sftp.CreateDirectory(remotePath);
}
sftp.UploadFile(file, remotePath + fileName);
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="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="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 + ) && 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)
{
throw new Exception(string.Format("SFTP文件移动失败,原因:{0}", ex.Message));
}
}
#endregion #region 删除SFTP文件
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 创建目录
/// <summary>
/// 循环创建目录
/// </summary>
/// <param name="remotePath">远程目录</param>
private void CreateDirectory(string remotePath)
{
try
{
string[] paths = remotePath.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
string curPath = "/";
for (int i = ; i < paths.Length; i++)
{
curPath += paths[i];
if (!sftp.Exists(curPath))
{
sftp.CreateDirectory(curPath);
}
if (i < paths.Length - )
{
curPath += "/";
}
}
}
catch (Exception ex)
{
throw new Exception(string.Format("创建目录失败,原因:{0}", ex.Message));
}
}
#endregion
}
2、调用
/// <summary>
/// 上传图片
/// </summary>
/// <param name="file"></param>
/// <returns></returns>
public ActionResult UploadImage(int HotelID, HttpPostedFileBase file)
{
if (file == null)
{
return Json(new { code = , message = "请先选择图片" });
}
string batchNo = System.Guid.NewGuid().ToString();
string fileWebPath = "/upload/" + HotelID + "/";
string directory = Request.MapPath("~" + fileWebPath);
string fileName = batchNo + Path.GetExtension(file.FileName);
if (!Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
}
string fileFullName = Path.Combine(directory, fileName);
try
{
file.SaveAs(fileFullName);
//上传SFTP
SFTPHelper sftp = new SFTPHelper();
sftp.Put(fileFullName, "/home/cmsimages/ads/cmsimages/choice/" + HotelID + "/", fileName);
return Json(new { code = , message = fileWebPath + fileName });
}
catch (Exception e)
{
return Json(new { code = , message = e.Message });
}
}
3、Renci.SshNet.dll下载链接:
https://download.csdn.net/download/jiduxiaozhang12345/10695019
c# 使用Renci.SshNet.dll操作SFTP总结的更多相关文章
- FileUpload 上传文件,并实现c#使用Renci.SshNet.dll实现SFTP文件传输
fileupload上传文件和jquery的uplodify控件使用方法类似,对服务器控件不是很熟悉,记录一下. 主要是记录新接触的sftp文件上传.服务器环境下使用freesshd搭建好环境后,wi ...
- 未能加载文件或程序集“Renci.SshNet, Version=2016.1.0.0, Culture=neutral, PublicKeyToken=……”
emmmm~ 这是一个让人烦躁有悲伤的问题~ 背景 我也不知道什么原因,用着用着,正好好的,就突然报了这种问题~ 未能加载文件或程序集“Renci.SshNet, Version=2016.1.0.0 ...
- C#访问SFTP:Renci.SshNet.Async
SFTP是SSH File Transfer Protocol的缩写,安全文件传送协议.安全文件传送协议.可以为传输文件提供一种安全的网络的加密方法.sftp 与 ftp 有着几乎一样的语法和功能. ...
- sftp上传文件(Renci.SshNet)和代理上传
引用Renci.SshNet这个 封装的sftp类 public class SFTPHelper { #region 字段或属性 private SftpClient sftp; /// <s ...
- .net 操作sftp服务器
因为项目的需要,整理了一段C#操作sftp的方法. 依赖的第三方类库名称为:SharpSSH 1.1.1.13. 代码如下: 1: using System; 2: using System.Coll ...
- Renci.SshNet在Linux运维的应用
SSH.NET是一个.net的SSH应用库,支持并发.该库最新的代码可以从github上下载下来,比Sharp.SSH更新的频繁.它可以模拟ssh登陆,类似xshell.putty等工具.不过有更多的 ...
- jxl.dll操作总结
1)Jxl是一个开源的Java Excel API项目,通过Jxl,Java可以很方便的操作微软的Excel文档.除了Jxl之外,还有Apache的一个POI项目,也可以操作Excel,两者相比之下: ...
- Asp.Net使用org.in2bits.MyXls.dll操作excel的应用
首先下载org.in2bits.MyXls.dll(自己的在~\About ASP.Net\Asp.Net操作excel) 添加命名空间: using org.in2bits.MyXls;using ...
- C#使用OpcNetApi.dll和OpcNetApi.Com.dll操作OPC
本人学习了一下.Net,恰好,51自学网,又要用这个.而网上很多VC6,VB6,VB .Net的但,很少C#的.现在研究一下,给出例子: 测试平台,是VS2008,KEPServer,OpcNetAp ...
随机推荐
- Win7 指定以某个用户运行某个程式
登陆的是用户A,想要以用户B执行某个程式,可以在cmd命令符下执行以下语句 runas /user:Domain\UserB /savecred notepad.exe 说明:/user:的后面即为 ...
- 简易轮播图、内含定时器。熟练JS操作
HTML部分: <!DOCTYPE html><html lang="en"><head> <meta charset="UTF ...
- PL/SQL变量的作用域和可见性
变量的作用域和可见性设计变量在块中的位置,不同的位置使得变量具有不同的有效性与可访问性. 变量的作用域是指可以使用变量的程序单元部分,可以是包和子程序包等. 当一个变量在它的作用域中可以用一个不限定的 ...
- 2018年山东省省队集训 Round 1 Day 2简要题解
从这里开始 Problem A 生日礼物 Problem B 咕咕 Problem C 解决npc (相信来看这篇博客的人都有题面) T2以为可以线性递推,然后花了两个小时.然后想了两个小时T1,会了 ...
- FL Studio中音频ASIO4ALL的设置
上期我们讲解了FL Studio中音频的相关设置,今天我们来进一步讲解音频设置中的ASIO4ALL的设置,FL Studio安装包括FL Studio ASIO和第三方ASIO驱动程序ASIO4ALL ...
- HDU 1024 Max Sum Plus Plus【DP】
Now I think you have got an AC in Ignatius.L's "Max Sum" problem. To be a brave ACMer, we ...
- 【转载】JVM 学习——垃圾收集器与内存分配策略
本文主要是对<深入理解java虚拟机 第二版>第三章部分做的总结,文章中大部分内容都来自这章内容,也是博客 JVM 学习的第二部分. 简述 说到垃圾收集(Garbage Collectio ...
- .Net dependent configuration
error info: 解决方案:在.exe.config文件中配置Newtonsoft.Json所用版本 <runtime> <assemblyBinding xmlns=&quo ...
- 记一次VM虚拟机Ubuntu无法联网问题
突然ubuntu获取不到ipv4地址,手动设置静态ip也ping不通本机, 在网上试了一堆的方法也不行,就怀疑是vm设置问题了.因为 作业环境我的VM需要经常性的改变桥接的网卡,所以检查了一 下这里, ...
- Confluence-6.10.0+Jira-7.13+Crowd-3.2.1最全破解文档,附下载包
=========================================2019.4.19更改================================================ ...