C#利用SFTP實現(xiàn)上傳下載
更新時間:2017年10月27日 14:27:33 作者:白鐵
這篇文章主要為大家詳細(xì)介紹了C#利用SFTP實現(xiàn)上傳下載的相關(guān)代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下
sftp是ftp協(xié)議的升級版本,是犧牲上傳速度為代價,換取安全性能,本人開始嘗試使用Tamir.SharpSSH.dll但它對新版本的openssh 不支持,所有采用Ssh.Net方式 需要依賴:Renci.SshNet.dll 下載鏈接
/// <summary>
/// SFTP操作類
/// </summary>
public class SFTPHelper
{
#region 字段或?qū)傩?
private SftpClient sftp;
/// <summary>
/// SFTP連接狀態(tài)
/// </summary>
public bool Connected { get { return sftp.IsConnected; } }
#endregion
#region 構(gòu)造
/// <summary>
/// 構(gòu)造
/// </summary>
/// <param name="ip">IP</param>
/// <param name="port">端口</param>
/// <param name="user">用戶名</param>
/// <param name="pwd">密碼</param>
public SFTPHelper(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">遠(yuǎn)程路徑</param>
public void Put(string localPath, string remotePath)
{
try
{
using (var file = File.OpenRead(localPath))
{
Connect();
sftp.UploadFile(file, remotePath);
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">遠(yuǎn)程路徑</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)
{
// 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="remoteFile">遠(yuǎn)程路徑</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">遠(yuǎn)程目錄</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">舊遠(yuǎn)程路徑</param>
/// <param name="newRemotePath">新遠(yuǎn)程路徑</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
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Unity通用泛型單例設(shè)計模式(普通型和繼承自MonoBehaviour)
這篇文章主要介紹了Unity通用泛型單例設(shè)計模式,分為普通型和繼承MonoBehaviour,幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-07-07
C#如何使用SHBrowseForFolder導(dǎo)出中文文件夾詳解
這篇文章主要給大家介紹了關(guān)于C#如何使用SHBrowseForFolder導(dǎo)出中文文件夾的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)合作工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-11-11

