C#利用SFTP實(shí)現(xiàn)上傳下載
sftp是ftp協(xié)議的升級(jí)版本,是犧牲上傳速度為代價(jià),換取安全性能,本人開始嘗試使用Tamir.SharpSSH.dll但它對(duì)新版本的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 移動(dòng)SFTP文件
/// <summary>
/// 移動(dòng)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文件移動(dòng)失敗,原因:{0}", ex.Message));
throw new Exception(string.Format("SFTP文件移動(dòng)失敗,原因:{0}", ex.Message));
}
}
#endregion
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#對(duì)DataTable里數(shù)據(jù)排序的方法
在日常開發(fā)過程中,有一個(gè)DataTable集合,里面有很多字段,現(xiàn)在要求針對(duì)某一列進(jìn)行排序,如果該列為數(shù)字的話,進(jìn)行ASC即可實(shí)現(xiàn),但是該字段類型為string,此時(shí)排序就有點(diǎn)不正確了2013-11-11
關(guān)于C#中使用Oracle存儲(chǔ)過程返回結(jié)果集的問題
Oracle中可以使用游標(biāo)(Cursor)對(duì)數(shù)據(jù)集進(jìn)行操作,但在存儲(chǔ)過程輸出參數(shù)中直接使用Cursor錯(cuò)誤,下面小編給大家?guī)砹薈#中使用Oracle存儲(chǔ)過程返回結(jié)果集的問題,感興趣的朋友一起看看吧2021-10-10
C#窗體-數(shù)據(jù)庫(kù)連接及登錄功能的實(shí)現(xiàn)案例
這篇文章主要介紹了C#窗體-數(shù)據(jù)庫(kù)連接及登錄功能的實(shí)現(xiàn)案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-12-12
Unity shader實(shí)現(xiàn)自由放大縮小效果
這篇文章主要為大家詳細(xì)介紹了Unity shader實(shí)現(xiàn)自由放大縮小效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-02-02
C#程序連接數(shù)據(jù)庫(kù)及讀取數(shù)據(jù)庫(kù)中字段的簡(jiǎn)單方法總結(jié)
包括C#連接Access、Oracle或者SQL Server,這里整理了一些C#連接數(shù)據(jù)庫(kù)及從讀取數(shù)據(jù)庫(kù)中字段的簡(jiǎn)單方法總結(jié),需要的朋友可以參考下2016-05-05

