C#利用SFTP實(shí)現(xiàn)上傳下載
sftp是ftp協(xié)議的升級版本,是犧牲上傳速度為代價(jià),換取安全性能,本人開始嘗試使用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í)有所幫助,也希望大家多多支持我們。
欄 目:C#教程
下一篇:C# 操作PostgreSQL 數(shù)據(jù)庫的示例代碼
本文標(biāo)題:C#利用SFTP實(shí)現(xiàn)上傳下載
本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/5426.html
您可能感興趣的文章
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻 器左下角滾動新聞效果的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#實(shí)現(xiàn)讀取注冊表監(jiān)控當(dāng)前操作系統(tǒng)已安裝軟件變化的方法
- 01-10C#實(shí)現(xiàn)多線程下載文件的方法
- 01-10C#實(shí)現(xiàn)Winform中打開網(wǎng)頁頁面的方法
- 01-10C#實(shí)現(xiàn)遠(yuǎn)程關(guān)閉計(jì)算機(jī)或重啟計(jì)算機(jī)的方法
- 01-10C#自定義簽名章實(shí)現(xiàn)方法
- 01-10C#文件斷點(diǎn)續(xù)傳實(shí)現(xiàn)方法
- 01-10winform實(shí)現(xiàn)創(chuàng)建最前端窗體的方法


閱讀排行
本欄相關(guān)
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁無法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻 器左下角滾動新
- 01-10C#停止線程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 01-10C#中split用法實(shí)例總結(jié)
- 08-05織夢dedecms什么時(shí)候用欄目交叉功能?
- 04-02jquery與jsp,用jquery
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-10delphi制作wav文件的方法
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文