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总结的更多相关文章

  1. FileUpload 上传文件,并实现c#使用Renci.SshNet.dll实现SFTP文件传输

    fileupload上传文件和jquery的uplodify控件使用方法类似,对服务器控件不是很熟悉,记录一下. 主要是记录新接触的sftp文件上传.服务器环境下使用freesshd搭建好环境后,wi ...

  2. 未能加载文件或程序集“Renci.SshNet, Version=2016.1.0.0, Culture=neutral, PublicKeyToken=……”

    emmmm~ 这是一个让人烦躁有悲伤的问题~ 背景 我也不知道什么原因,用着用着,正好好的,就突然报了这种问题~ 未能加载文件或程序集“Renci.SshNet, Version=2016.1.0.0 ...

  3. C#访问SFTP:Renci.SshNet.Async

    SFTP是SSH File Transfer Protocol的缩写,安全文件传送协议.安全文件传送协议.可以为传输文件提供一种安全的网络的加密方法.sftp 与 ftp 有着几乎一样的语法和功能. ...

  4. sftp上传文件(Renci.SshNet)和代理上传

    引用Renci.SshNet这个 封装的sftp类 public class SFTPHelper { #region 字段或属性 private SftpClient sftp; /// <s ...

  5. .net 操作sftp服务器

    因为项目的需要,整理了一段C#操作sftp的方法. 依赖的第三方类库名称为:SharpSSH 1.1.1.13. 代码如下: 1: using System; 2: using System.Coll ...

  6. Renci.SshNet在Linux运维的应用

    SSH.NET是一个.net的SSH应用库,支持并发.该库最新的代码可以从github上下载下来,比Sharp.SSH更新的频繁.它可以模拟ssh登陆,类似xshell.putty等工具.不过有更多的 ...

  7. jxl.dll操作总结

    1)Jxl是一个开源的Java Excel API项目,通过Jxl,Java可以很方便的操作微软的Excel文档.除了Jxl之外,还有Apache的一个POI项目,也可以操作Excel,两者相比之下: ...

  8. Asp.Net使用org.in2bits.MyXls.dll操作excel的应用

    首先下载org.in2bits.MyXls.dll(自己的在~\About ASP.Net\Asp.Net操作excel) 添加命名空间: using org.in2bits.MyXls;using ...

  9. C#使用OpcNetApi.dll和OpcNetApi.Com.dll操作OPC

    本人学习了一下.Net,恰好,51自学网,又要用这个.而网上很多VC6,VB6,VB .Net的但,很少C#的.现在研究一下,给出例子: 测试平台,是VS2008,KEPServer,OpcNetAp ...

随机推荐

  1. vue强制刷新组件

    <component v-if="hackReset"></component>(组件名称) data:hackReset (事件执行) this.hack ...

  2. Python 运维之路

    第一章:Python基础知识 1.Python 变量了解 .Python 二进制 .Python 字符编码 4.Python if条件判断 5.Python while循环 6.Python for循 ...

  3. markdown code test - markdown code test - markdown code test - markdown code test

    目录 主要代码 bottom cc 主要代码 <div class="tocCol1 js-toc"></div> <a>bc</a> ...

  4. 51nod1363 最小公倍数之和

    题目描述 给出一个n,求1-n这n个数,同n的最小公倍数的和. 例如:n = 6,1,2,3,4,5,6 同6的最小公倍数分别为6,6,6,12,30,6,加在一起 = 66. 由于结果很大,输出Mo ...

  5. 使用卷积神经网络CNN完成验证码识别

    gen_sample_by_captcha.py 生成验证码图片 # -*- coding: UTF-8 -*- """ 使用captcha lib生成验证码(前提:pi ...

  6. SAP ERP SD模块中维护销售人员

    SAP ERP SD模块中维护销售人员信息并分配销售组织   分类: SAPHCM用户指南   在SAP ERP系统,销售和分销(SD)模块中需要创建销售人员(Sales Personnels)消息, ...

  7. TF-IDF 提取关键词

    <?php class Document { protected $words; protected $tf_matrix; protected $tfidf_matrix; public fu ...

  8. C语言--第2次作业

    1.本章学习总结 1.1思维导图 1.2本章学习体会及本章代码量 1.2.1学习体会 不同于前几周简单的条件语句等,这一周开始学习循环结构for,while语句,甚至是多种语句嵌套使用,让我直接感受到 ...

  9. vue项目知识点总结

    一.vue中如何获取select被选中的id和对应的值. <!-- 下拉框 --> <div v-show="moreStore" class="sel ...

  10. Day 4 变量常量

    编辑语言的分类 编程语言,他是人与计算机沟通的一种介质 机器语言 计算机只认识0和1,为了和计算机沟通,你也得认识0和1 优点:执行效率快 缺点:普通人根本就写不了这种代码,开发效率低 汇编语言 他还 ...