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 198 199 200 201 202 203 204 205 206 207 208 209
| using NLog; using Renci.SshNet; using Renci.SshNet.Sftp; using ShellProgressBar; using System; using System.Collections.Generic; using System.IO; using System.Text; using System.Threading;
namespace PiSftpClient { public class SFtpHelper { private SftpClient sftp;
private static readonly Logger logger = LogManager.GetLogger("SFtpHelper");
public bool Connected { get { return this.sftp.IsConnected; } }
public SFtpHelper(string host, int port, string username, string password) { this.sftp = new SftpClient(host, port, username, password); }
public bool Connect() { bool result; try { bool flag = !this.Connected; if (flag) { this.sftp.Connect(); } result = true; } catch (Exception ex) { result = false; logger.Error(string.Format("连接SFTP失败,原因:{0}", ex.Message)); } return result; }
public void Disconnect() { try { bool flag = this.sftp != null && this.Connected; if (flag) { this.sftp.Disconnect(); } } catch (Exception ex) { logger.Error(string.Format("断开SFTP失败,原因:{0}", ex.Message)); } }
public void Put(string localPath, string remotePath) { try { using (FileStream fileStream = File.OpenRead(localPath)) { this.sftp.UploadFile(fileStream, remotePath, null); } } catch (Exception ex) { logger.Error(string.Format("SFTP文件上传失败,原因:{0}", ex.Message)); } }
public void Put(byte[] fileByteArr, string remotePath) { try { Stream input = new MemoryStream(fileByteArr); this.sftp.UploadFile(input, remotePath, null); } catch (Exception ex) { logger.Error(string.Format("SFTP文件上传失败,原因:{0}", ex.Message)); } }
public void DownloadFile(string remotePath, string localPath) { try { this.Connect(); MemoryStream stream = new MemoryStream(); this.sftp.DownloadFile(remotePath, stream, UpdateProgress); File.WriteAllBytes(localPath, stream.ToArray()); } catch (Exception ex) { logger.Error(string.Format("SFTP文件获取失败,原因:{0}", ex.Message)); } }
private void UpdateProgress(ulong obj) { }
public void Delete(string remoteFile) { try { this.sftp.Delete(remoteFile); } catch (Exception ex) { logger.Error(string.Format("SFTP文件删除失败,原因:{0}", ex.Message)); } }
public List<string> GetFileList(string remotePath, string fileSuffix) { List<string> result; try { IEnumerable<SftpFile> enumerable = this.sftp.ListDirectory(remotePath, null); result = new List<string>(); foreach (SftpFile current in enumerable) { if (current.Name.EndsWith(fileSuffix)) { result.Add(current.Name); } } } catch (Exception ex) { result = new List<string>(); logger.Error(string.Format("SFTP文件列表获取失败,原因:{0}", ex.Message)); } return result; }
public void Move(string oldRemotePath, string newRemotePath) { try { this.sftp.RenameFile(oldRemotePath, newRemotePath); } catch (Exception ex) { logger.Error(string.Format("SFTP文件移动失败,原因:{0}", ex.Message)); } } } }
|