一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - C# - C#利用SFTP實現(xiàn)上傳下載

C#利用SFTP實現(xiàn)上傳下載

2022-01-25 14:11白鐵 C#

這篇文章主要為大家詳細介紹了C#利用SFTP實現(xiàn)上傳下載的相關(guān)代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下

sftp是ftp協(xié)議的升級版本,是犧牲上傳速度為代價,換取安全性能,本人開始嘗試使用Tamir.SharpSSH.dll但它對新版本的openssh 不支持,所有采用Ssh.Net方式 需要依賴:Renci.SshNet.dll 下載鏈接

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/// <summary>
  /// SFTP操作類
  /// </summary>
  public class SFTPHelper
  {
    #region 字段或?qū)傩?/code>
    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">遠程路徑</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">遠程路徑</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">遠程路徑</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">遠程目錄</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">舊遠程路徑</param>
    /// <param name="newRemotePath">新遠程路徑</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)容,希望對大家的學習有所幫助,也希望大家多多支持服務(wù)器之家。

原文鏈接:http://www.cnblogs.com/cbread/p/6202069.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 91人人在线| 免费看视频高清在线观看 | 欧美爽妇 | 日本肉体xxxx | 国产成人精品免费久久久久 | 亚洲人成综合在线播放 | 动漫人物差差插曲漫画 | 成年人在线视频免费观看 | 亚洲精品私拍国产福利在线 | 古装一级无遮挡毛片免费观看 | 福利国产精品 | 日韩一级片免费观看 | 99热在线获取最新地址 | 国内体内she精视频免费 | 草草剧场 | 好大好爽好舒服视频 | 国产在线观看精品香蕉v区 国产在线观看a | 91精品国产91热久久p | 美女逼逼软件 | 国内体内she精视频免费 | 日韩妹妹| 日韩一区二区三区在线 | 亚洲国产综合另类视频 | 99精品视频免费在线观看 | 国产日本免费 | 成人资源在线观看 | 日本五十路六十30人8时间 | 草草国产成人免费视频 | 国产中文字幕 | 变形金刚第一部 | 护士xxxx | 97大香伊在人人线色 | jiuse在线 | 欧美三级做爰全过程 | 亚州vs欧州vs日| 日本视频在线免费观看 | 99精品视频在线观看re | 99精品视频免费 | 免费又爽又黄禁片视频在线播放 | 免费看日本| 性做久久久久久久久老女人 |