国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

.net 操作sftp服務器

2019-11-17 02:04:38
字體:
來源:轉載
供稿:網友

.net 操作sftp服務器

因為項目的需要,整理了一段C#操作sftp的方法。

依賴的第三方類庫名稱為:SharpSSH 1.1.1.13.

代碼如下:

   1:  using System;
   2:  using System.Collections.Generic;
   3:  using System.Linq;
   4:  using System.Text;
   5:  using System.Collections.Specialized;
   6:  using System.Configuration;
   7:  using Tamir.SharpSsh;
   8:  using System.IO;
   9:  using Tamir.SharpSsh.jsch;
  10:   
  11:  namespace CET.Finance.SftpWinService.Common
  12:  {
  13:      /// <summary>
  14:      /// 訪問Sftp服務器方法(憑證請在config文件中配置)
  15:      /// </summary>
  16:      public class SftpClient : IDisposable
  17:      {
  18:          #region PRoperties
  19:   
  20:          /// <summary>
  21:          /// 主機名或ip
  22:          /// </summary>
  23:          public string HostName { get; private set; }
  24:          /// <summary>
  25:          /// 用戶名
  26:          /// </summary>
  27:          public string UserName { get; private set; }
  28:          /// <summary>
  29:          /// 密碼
  30:          /// </summary>
  31:          public string PassWord { get; private set; }
  32:   
  33:          /// <summary>
  34:          /// 端口號(默認端口為22)
  35:          /// </summary>
  36:          public int Port { get; private set; }        
  37:   
  38:          #endregion
  39:   
  40:          private static readonly string defRemotePath = "/";//默認操作是都是從根目錄開始。
  41:          private ChannelSftp m_sftp;
  42:          private session m_session;
  43:          Channel m_channel;
  44:   
  45:          /// <summary>
  46:          /// 從配置文件中加載憑證信息
  47:          /// </summary>       
  48:          public SftpClient()
  49:          {
  50:              var config = ConfigurationManager.GetSection("SftpServer") as NameValueCollection;
  51:              this.HostName = config["host_name"];
  52:              this.UserName = config["user_name"];
  53:              this.Password = config["password"];
  54:              this.Port = Convert.ToInt32(config["port"] ?? "22");//默認端口為22    
  55:              this.ConnectSftp();
  56:          }
  57:   
  58:          #region Events
  59:   
  60:          /// <summary>
  61:          /// SFTP獲取文件   
  62:          /// </summary>
  63:          /// <param name="remotePath"></param>
  64:          /// <param name="localPath"></param>
  65:          /// <returns></returns>
  66:   
  67:          public bool Get(string remotePath, string localPath)
  68:          {
  69:              try
  70:              {
  71:                  string fullRemotePath = defRemotePath + remotePath.TrimStart('/');
  72:                  Tamir.SharpSsh.java.String src = new Tamir.SharpSsh.java.String(fullRemotePath);
  73:                  Tamir.SharpSsh.java.String dst = new Tamir.SharpSsh.java.String(localPath);
  74:                  m_sftp.get(src, dst);
  75:                  return true;
  76:              }
  77:              catch
  78:              {
  79:                  return false;
  80:              }
  81:          }
  82:   
  83:          /// <summary>
  84:          ///SFTP存放文件   
  85:          /// </summary>
  86:          /// <param name="localPath"></param>
  87:          /// <param name="remotePath"></param>
  88:          /// <returns></returns>
  89:          public void Put(string localPath, string remotePath)
  90:          {
  91:              Tamir.SharpSsh.java.String src = new Tamir.SharpSsh.java.String(localPath);
  92:              string fullRemotePath = defRemotePath + remotePath.TrimStart('/');
  93:              Tamir.SharpSsh.java.String dst = new Tamir.SharpSsh.java.String(fullRemotePath);
  94:              m_sftp.put(src, dst);
  95:          }
  96:   
  97:   
  98:          /// <summary>
  99:          /// 刪除SFTP文件 
 100:          /// </summary>
 101:          /// <param name="remoteFile"></param>
 102:          /// <returns></returns>
 103:   
 104:          public void Delete(string remoteFile)
 105:          {
 106:              string fullRemotePath = defRemotePath + remoteFile.TrimStart('/');
 107:              m_sftp.rm(fullRemotePath);
 108:          }
 109:          /// <summary>
 110:          /// 獲取SFTP文件列表   
 111:          /// </summary>
 112:          /// <param name="remotePath"></param>
 113:          /// <param name="fileType">文件后綴名稱(.txt)</param>
 114:          /// <returns></returns>
 115:          public List<string> GetFileList(string remotePath, string fileType)
 116:          {
 117:              List<string> objList = new List<string>();
 118:              string fullRemotePath = defRemotePath + remotePath.TrimStart('/');
 119:              if (DirExist(fullRemotePath))
 120:              {
 121:                  Tamir.SharpSsh.java.util.Vector vvv = m_sftp.ls(fullRemotePath);
 122:                  foreach (Tamir.SharpSsh.jsch.ChannelSftp.LsEntry QQq in vvv)
 123:                  {
 124:                      string sss = qqq.getFilename();
 125:                      if (fileType.Contains(Path.GetExtension(sss)))
 126:                      { 
 127:                          objList.Add(sss);
 128:                      }                  
 129:                  }
 130:              }
 131:              return objList;
 132:          }
 133:   
 134:          /// <summary>
 135
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 尤溪县| 常州市| 奉贤区| 通海县| 固安县| 溧水县| 分宜县| 林州市| 阳西县| 乌拉特前旗| 芷江| 威海市| 金平| 曲阳县| 安吉县| 鄢陵县| 封丘县| 张家港市| 郯城县| 呼玛县| 织金县| 竹山县| 北宁市| 疏勒县| 宿迁市| 浦县| 繁峙县| 上蔡县| 伊通| 云林县| 凤阳县| 嘉荫县| 木兰县| 北票市| 吉木萨尔县| 深圳市| 抚顺市| 独山县| 五寨县| 建昌县| 太原市|