.net操作sftp服务器

发表于:2015-4-22 11:14

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:Ants    来源:51Testing软件测试网采编

  因为项目的需要,整理了一段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 TestSftp
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:          }
56:
57:          #region Events
58:
59:          /// <summary>
60:          /// SFTP获取文件
61:          /// </summary>
62:          /// <param name="remotePath"></param>
63:          /// <param name="localPath"></param>
64:          /// <returns></returns>
65:
66:          public bool Get(string remotePath, string localPath)
67:          {
68:              try
69:              {
70:                  string fullRemotePath = defRemotePath + remotePath.TrimStart('/');
71:                  Tamir.SharpSsh.java.String src = new Tamir.SharpSsh.java.String(fullRemotePath);
72:                  Tamir.SharpSsh.java.String dst = new Tamir.SharpSsh.java.String(localPath);
73:                  m_sftp.get(src, dst);
74:                  return true;
75:              }
76:              catch
77:              {
78:                  return false;
79:              }
80:          }
81:
82:          /// <summary>
83:          ///SFTP存放文件
84:          /// </summary>
85:          /// <param name="localPath"></param>
86:          /// <param name="remotePath"></param>
87:          /// <returns></returns>
88:          public void Put(string localPath, string remotePath)
89:          {
90:              Tamir.SharpSsh.java.String src = new Tamir.SharpSsh.java.String(localPath);
91:              string fullRemotePath = defRemotePath + remotePath.TrimStart('/');
92:              Tamir.SharpSsh.java.String dst = new Tamir.SharpSsh.java.String(fullRemotePath);
93:              m_sftp.put(src, dst);
94:          }
95:
96:
21/212>
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号