本文主要是C#利用Renci.SshNet類庫實現SFTP文件上傳、下載等功能,主要是要引用第三方類庫Renci.SshNet.dll。
注意:
1、Renci.SshNet.dll只支持.net framework 4.0及其以上版本。如需開發引用,需安裝VS2013以上版本,如需使用,需安裝.net framework 4.0
2、Renci.SshNet支持sftp基於ssh協議,同時支持sftp不基於ssh協議
以下是SFTPOperation類,實現了對文件的操作,可供參考。
using System;
using System.Collections.Generic;
using System.Text;
using System;
using System.Collections;
using System.IO;
using Renci.SshNet;
namespace SFTPHelper
{
/************************描述 SFTP操作類******************************************
**創建者 : aaa
**創建時間: 2015-03-11
**描述 : SFTP操作類
*********************************************************************************/
/// <summary>
/// SFTP操作類
/// </summary>
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)
{
//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 bool Put(string localPath, string remotePath)
{
bool result = false;
try
{
using (var file = File.OpenRead(localPath))
{
Connect();
sftp.UploadFile(file, remotePath);
Disconnect();
result = true;
}
}
catch (Exception ex)
{
//TxtLog.WriteTxt(CommonMethod.GetProgramName(), string.Format("SFTP文件上傳失敗,原因:{0}", ex.Message));
throw new Exception(string.Format("SFTP文件上傳失敗,原因:{0}", ex.Message));
}
return result;
}
#endregion
#region SFTP獲取文件
/// <summary>
/// SFTP獲取文件
/// </summary>
/// <param name="remotePath">遠程路徑</param>
/// <param name="localPath">本地路徑</param>
public bool Get(string remotePath, string localPath)
{
bool result = false;
try
{
Connect();
var byt = sftp.ReadAllBytes(remotePath);
Disconnect();
File.WriteAllBytes(localPath, byt);
result = true;
}
catch (Exception ex)
{
//TxtLog.WriteTxt(CommonMethod.GetProgramName(), string.Format("SFTP文件獲取失敗,原因:{0}", ex.Message));
throw new Exception(string.Format("SFTP文件獲取失敗,原因:{0}", ex.Message));
}
return result;
}
#endregion
#region 刪除SFTP文件
/// <summary>
/// 刪除SFTP文件
/// </summary>
/// <param name="remoteFile">遠程路徑</param>
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文件列表
/// <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)
{
//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)
{
//TxtLog.WriteTxt(CommonMethod.GetProgramName(), string.Format("SFTP文件移動失敗,原因:{0}", ex.Message));
throw new Exception(string.Format("SFTP文件移動失敗,原因:{0}", ex.Message));
}
}
#endregion
}
}
vb.net
</pre><pre name="code" class="vb">Dim sftp As SFTPOperation = New SFTPOperation("192.168.1.10",22, abc, 123456) 'sftp IP,端口號,用戶名,密碼
sftp.Connect()
Dim sftpFileName As String = "/sftpFile/aa.txt" 'sftp上面的文件路徑及文件名
sftp.Put("E:\loadFile\abc.txt", sftpFileName) '上傳:本地路徑及本地文件名,sftp上文件路徑及文件名。上傳之後可自動改名,也可以不改名
<span style="font-family: Arial, Helvetica, sans-serif;">sftp.Get("/sftpFile/aa.txt","E:\loadFile\abc.txt") '下載:sftp上文件路徑及文件名,本地路徑及本地文件名。下載之後可自動改名,也可以不改名</span>
<span style="font-family: Arial, Helvetica, sans-serif;">sftp.Disconnect()</span>
實例下載地址:利用Renci.SshNet類庫實現sftp文件操作